C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Use TextChanged with logical tests to make your program responsive and reactive to your users' actions.
Steps: To make a new TextChanged event, open Visual Studio—and follow these steps.
Then: Scroll down to where the word TextChanged appears. Double-click in the space next to the TextChanged label.
Press TAB twice after typing: C#
// 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);
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.
Fragment that uses Text property in TextChanged: C#
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)
}
Fragment that changes Enabled property on TextChanged: C#
void textBox1_TextChanged(object sender, EventArgs e)
{
// Just something you may want to do after TextChanged.
openButton.Enabled = TextIsValid(textBox1.Text);
}
Note: The runtime filters out modifications that don't result in a different Text value.
Note: For displaying instant search results from a database, this event is ideal. Programs are fast and responsive.