TextBox is a very common control used in Winforms. Every window application always have this control. Recently I got chance to work with Textbox�s features other than common properties and methods. I am trying to consolidate what I learnt which is not a common. The common features generally we use are Text, MultiLine, Lines, PasswordChar, ReadOnly, TextAlign etc.
My requirement was to select a specific word in the textbox to select programtically, 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.
textBox1.SelectionStart = startIndex; textBox1.SelectionLength = length;
Both methods work fine. If we want to replace selected text or insert a text we should use TextBox.SelectedText property.
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.
This is a very useful when TextBox is being used for keyword entry or similar kind of functionality, where TextBox itself can suggest user some values and pre populate it. TextBox has AutoCompleteMode property which by default is None. There are three modes for autocomplete:
Suggest : This will display dropdown with suggested values and can be selected from there.
Append: This will start displaying values based on starting values you type. It will not show suggestion dropdown to choose from.
SuggestAppend: This is combined effect of Suggest Append.
We also need auto complete source. AutoCompleteSource serve that purpose. Source can be system resources like, browser history (URLs, file system, Files etc.). A custom source can also be provided. First need to specify AutoCompleteSource.CustomSource and then specify the custom list using property TextBox.AutoCompleteSource. See example below:
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection(); textBox1.AutoCompleteCustomSource.Add("Chris"); textBox1.AutoCompleteCustomSource.Add("Christeena"); textBox1.AutoCompleteCustomSource.Add("Tim Carpenter");
AutoCompleteSource.ListItems doesn�t work with TextBox or RichTextBox. It can be used for ComboBox.
There are a few more properties can be helpful:
|AcceptsReturn||Gets or sets a value indicating whether pressing ENTER in a multiline System.Windows.Forms.TextBox control creates a new line of text in the control or activates the default button for the form. Default value is false.|
|AcceptsTab||Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order. Default value is false.|
|Cut()||It will cut the selected text and store in Clipboard. This method can be used when we want to perform Undo(). SelectedText= string.Emtpy will behave similarly but this change is not Undoable.|
|AppendText()||Adds text at the end of current text of the textbox. It is similar to TextBox.Text += �new text�|
|Copy()||Copy the selected text in clipboard.|
These small things gives many great string manipulation functionalities, which we generally expect from StringBuilder. Whichever control is implementing TextBoxBase having same features like ComboBox, RichTextBox etc.
|Nitya Prakash Sharma has over 10 years of experience in .NET technology. He is currently working as Senior Consultant in industry. He is always keen to learn new things in Technology and eager to apply wherever is possible. He is also has interest in Photography, sketching and painting.