Home
Map
TextChanged EventUse the TextChanged event in Windows Forms. Set Enabled when text is changed.
WinForms
This page was last reviewed on Jun 14, 2023.
TextChanged. This is an event—it occurs when the text is modified in a TextBox. With it we make a program dependent on the value of a TextBox when the user types.
Some notes. We can look up something in a database and display instantly the results. Use TextChanged with logical tests to make your program responsive and reactive to your users' actions.
First, there are 2 ways to make a TextChanged event, or any Windows Forms event, in Visual Studio. The easiest way is with the UI in Visual Studio.
Step 1 Click on the TextBox and focus it to see the correct designer UI. On the right bottom, you should see the Properties pane.
Step 2 Look for the lightning bolt icon in the pane. After you find the lightning bolt, click on it.
Step 3 Scroll down to where the word TextChanged appears. Double-click in the space next to the TextChanged label.
Detail The other way to add TextChanged is to type the word "this.TextChanged +" into your form and then press tab twice.
// 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);
Text. Here we use TextChanged to automate changes in a program's UI. Let's say you want to show search results as the user types in a search box.
Detail Get the TextChanged event set up, and then we will use the Text property to show search results.
Tip You can modify aspects of the program with TextChanged. All of this code is custom and the functions must be defined.
Here We use the new TextChanged Text value and call a custom function that manages more aspects of the interface.
void textBox1_TextChanged(object sender, EventArgs e) { PopulateGrid(textBox1.Text); } void PopulateGrid(string queryStr) { dataGridView1.DataSource = _journal.GetSearchResults(queryStr); SetStatus(dataGridView1.Rows.Count); // Change status bar (not shown) }
Enabled. You can have a method that will toggle the Enabled property of various buttons in the form, such as OK and Cancel, depending on whether any items were found.
Info This will ensure the UI of your Windows Forms program is always correct and up-to-date.
void textBox1_TextChanged(object sender, EventArgs e) { // Just something you may want to do after TextChanged. openButton.Enabled = TextIsValid(textBox1.Text); }
Notes. TextChanged is triggered when the user types something into a TextBox, and the resulting value changes. It is also activated when the program itself changes the TextBox Text in code.
Note The runtime filters out modifications that don't result in a different Text value.
Synchronization. I recommend using TextChanged when you need to synchronize parts of the UI together with a Text value. Consider using the Enabled property along with TextChanged.
Summary. We used the TextChanged event in Windows Forms. TextChanged is a powerful and convenient way to automate interactive messages in your application.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 14, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.