Setting TextArea.Text breaks the undo manager. How to resolve that?

In a project, there’s a TextArea. The user may freely type text there, or (s)he can use a bevel button to choose among a list of predefined texts, which are then put in the TextArea (using TA.Text=ChoosenText).

I’m finding that once text is assigned to the TextArea, it’s no longer possible to undo. In other words, undo/redo only works for user-entered changes.

First of all, this doesn’t sound right to me. Changing the text programmatically, like in this example, it’s the user’s choice; so, at the very least, there must be a way to tell the control/OS to change the text with considering to add it to the current undo stack. The user being unable to undo a change because (s)he didn’t explicitly typed the characters, that’s odd.

I’ve searched thru this forum, and the main given answer is to use methods like RemoteControlMBS to simulate user typing. That’s not a satisfying answer, for several reasons. One being that the text may contain a character that is not even available in the current keyboard layout; another reason is that finding which keys combination would produce a given character involves looping from 1 to 127, for each possible modifiers combinations, so just typing a single character would already take time.

It would also be possible to reinvent the wheel and create a dummy undo manager. The OS already does it better than we could possibly do in Xojo.

Third option would be to put the text in the clipboard, use the Paste method and revert the clipboard. Xojo is limited with the clipboard, and enumerating what exists there isn’t trivial.
The point here is that Paste can change the text while adding a step to the undo stack, so it’s proving it’s doable.

How do other, non-Xojo apps, deal with modifying texts in a TextArea with keeping the undo stack?

You’ll probably need to work on your own undo manager at that point. There are several projects floating around that can probably help with that. It’s been a while since I’ve looked at them so I don’t have anything specific to point you to. Sorry.