["","windows~","27 Windows ","K,t.ewsssz(CsBC(CST~~}T~~(Y6#(CP6%P6/C*C.S}T~~}T~~","TextChanged"," 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. We can look up something in a database and display instantly the results. ","Tip: ","Use TextChanged with logical tests to make your program responsive and reactive to your users' actions.","Start."," First, there are two easy ways to make a TextChanged event, or any Windows Forms event, in Visual Studio. First we note the easiest way to use TextChanged events, and after that we demonstrate a manual way. ","Event ","event","Steps: ","To make a new TextChanged event, open Visual Studio\u2014and follow these steps.","Click on the TextBox"," and focus it to see the correct designer UI. On the right bottom, you should see the Properties pane. Look for the lightning bolt icon in the pane. After you find the lightning bolt, click on it. ","Then: ","Scroll down to where the word TextChanged appears. Double-click in the space next to the TextChanged label.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Add TextChanged."," The other way to add TextChanged is to type the word \"this.TextChanged +\" into your form and then press tab twice. This is an alternative to the previous way, and its drawback is that it can clutter your C# file you are working with. ","Press TAB twice after typing: C#","\n\n","// This is the manual way, which is an alternative to the first way.\n// Type 'this.TextChanged += ' into your form's constructor.\n// Then press TAB twice, and you will have a new event handler.\n","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. Get the TextChanged event set up, and then we will use the Text property to show search results.","You can modify aspects"," of the program with TextChanged. All of this code is custom and the functions must be defined. You can see an example of what you can do when TextChanged is triggered. ","Note: ","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#","\n\nvoid textBox1_TextChanged(object sender, EventArgs e)\n{\n PopulateGrid(textBox1.Text);\n}\n\nvoid PopulateGrid(string queryStr)\n{\n dataGridView1.DataSource = _journal.GetSearchResults(queryStr);\n SetStatus(dataGridView1.Rows.Count);"," // Change status bar (not shown)\n","}","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. You can tie the Enabled property to an expression so it is always accurate. ","Fragment that changes Enabled property on TextChanged: C#","\n\nvoid textBox1_TextChanged(object sender, EventArgs e)\n{","\n // Just something you may want to do after TextChanged.\n ","openButton.Enabled = TextIsValid(textBox1.Text);\n}","Trigger."," 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, and the resulting value is different. ","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. The final code block in this article shows how to run more complex methods when the TextChanged event fires. ","Note: ","For displaying instant search results from a database, this event is ideal. Programs are fast and responsive.","Summary."," We used the TextChanged event in Windows Forms. TextChanged is a powerful and convenient way to automate interactive messages in your application. Users appreciate it when they press a key and the program instantly responds. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]