Use TextChanged events in Windows Forms and C#. You want to make some part of your program dependent on the value of a TextBox when the user types. For example, when the text in a TextBox is changed, you want to look up something in a database and display the results instantly. You could also need to enable or disable a button based on the Text content.
For the solution in this article, we will use Visual Studio 2008 and C# with .NET 3.5, although many other versions of VS will be the same. The first part deals with the basics, and then we go further into some examples and finally some finer details.
There are two easy ways to make a TextChanged event (or any Windows Forms event) in Visual Studio. First I will show you the easiest way to use TextChanged events, and after that I will demonstrate a manual way. To make a new TextChanged event, open Visual Studio follow these steps.
The other way to use TextChanged is to type the word "this.TextChanged +" into your form and then press tab twice. This is an alternative to the previously shown way, and its drawback is that it can clutter your C# file you are working with, but it can also allow some different programming techniques.
// This is the manual way, which is an alternative to the first way. // Type 'this.TextChanged += ' into your form's constructor. // Then press TAB twice, and you will have a new event handler. this.TextChanged += new EventHandler(Form1_TextChanged);
Here I show how you can actually use TextChanged to automate changes in your program's UI. Let's say you want to show search results as the user types in a search box. Get the TextChanged event set up, and then we will use the Text property to show search results.
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Call PopulateGrid with the new Text value.
// Note that in TextChanged, the textBox1.Text value
// is already set to its new value.
PopulateGrid(textBox1.Text);
}
Next you can modify aspects of the program with TextChanged. All of this code is custom and the functions must be defined by you. What follows is an example of what you can do when TextChanged is triggered. We use the new TextChanged Text value and call a custom function that manages more aspects of the interface.
private void PopulateGrid(string queryStr)
{
// Call the GetSearchResults function with the text string.
// It will return a DataSet, which we can link to the DataSource property.
dataGridView1.DataSource = _journal.GetSearchResults(queryStr);
// We can call special functions to update the Count property number
// presented to our user. I will show SetupButtons later.
SetStatus(dataGridView1.Rows.Count); // Change status bar (not shown)
}
We can have a method will toggle the Enabled property of various buttons in the form, such as OK and Cancel, depending on whether any items were found. What I want to emphasize here is that we tie the Enabled property to an expression so that it is always accurate.
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Just something you may want to do after TextChanged.
openButton.Enabled = TextIsValid(textBox1.Text);
}
TextChanged is triggered when the user types something into a TextBox, and the resulting value is different. It is also activated when the program itself changes the TextBox Text in code--and the resulting value is different. The runtime filters out modifications that don't result in a different Text value.
TextChanged is a powerful and convenient way to automate interactive messages in your application. It is my observation that users love it when they press a key and the program responds instantly. Make the user experience better and use TextChanged like a pro.
I recommend using TextChanged when you need to synchronize parts of the UI together with a Text value. The final code block I showed in this article illustrates how you can run more complex methods when the TextChanged event fires, like retrieving data from a database. For displaying instant search results from a database, this event is ideal.