TX Text Control .NET: Undo and the Selection Object
There are two ways to manipulate a range of text: A text range is selected using TextControl.Select. This selection is changed using the TextControl.Selection property that manipulates the current selection. A new Selection object is created and assigned to the TextControl.Selection property. 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 =…

There are two ways to manipulate a range of text:
- A text range is selected using TextControl.Select. This selection is changed using the TextControl.Selection property that manipulates the current selection.
- A new Selection object is created and assigned to the TextControl.Selection property.
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:
- A selection is visible on the screen. The text is visually selected with the transparent blue background in order to be formatted.
- Each change of the selection is counted as a single Undo history step.
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 above 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;
This is counted as a single Undo step and there is no visible selection background.
Related Posts
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
ASP.NETJavaScriptDocument Editor
Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More
This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.