SELECTION PROGRAMMATICALLY
My requirement was to select a specific word in the textbox to select programmatically, as I was implementing spelling check functionality. TextBox is inherited from TextBoxBase class and general text manipulation functions can be performed with methods and properties provided by base class. There are two ways to implement selection:
TextBox.Select(int startIndex, int length) : provide starting index and length of the text to be selected.
Using Properties.
1textBox1.SelectionStart = startIndex;
2
3textBox1.SelectionLength = length;
Both methods work fine. If we want to replace selected text or insert a text we should use TextBox.SelectedText property.
UNDOABLE TEXT CHANGE
TextBoxBase exposes a method Undo() to revert last change. It is can be used like toggle of changes Undo/Redo. Calling first time will undo the last text change done, but calling this method again will redo the change. As I mentioned in above method that we can change the selected text or insert the text by changing value of TextBox.SelectedText property. Updating the text using this property cannot be undo with Undo() method. To make text change undoable, one should use TextBox.Paste() method. You can paste method at selected section from Clipboard or constant/variable value. To determine the Undo will work or not, TextBoxBase class provide CanUndo property.