TXTextControl.Selection and Undo

There are two ways to manipulate a range of text:

Most users are going with the first, more obvious approach. A range of text is selected and changed using a sequence of Selection properties:

textControl1.Select(0, 5);
textControl1.Selection.Bold = true;
textControl1.Selection.Italic = true;
textControl1.Selection.Text = "New Text";

This works very reliably and can be used in most scenarios. But this approach has two drawbacks:

In the above code, three undo steps are required to revert to the previous state.

So, what if we could modify the Selection virtually before applying to our TextControl instance? This is what the second approach below does.

A new virtual Selection object is created and manipulated using the same properties. After the object is finished, it will be assigned to the Selection property of our TextControl instance.

TXTextControl.Selection sel = new TXTextControl.Selection();
sel.Text = "New Text";
sel.Bold = true;
sel.Italic = true;
textControl1.Selection = sel;
[Visual Basic]
Dim sel As New TXTextControl.Selection()
sel.Text = "New Text"
sel.Bold = True
sel.Italic = True
textControl1.Selection = sel

This is counted as a single Undo step and there is no visible selection background.

<< Back