checking text box for textchanged

Using c#. How do you check to see if the value in a textbox has changed. I want to only update my db if the text has changed, for say in a qty box, and I thought I could do if(_myControl.TextChanged) but I get and error saying it can only be used on a certain side of some operators.

thanks in advance!!
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

atxondoConnect With a Mentor Commented:
 It´s true an event will be thrown when a button is pushed. But you can use a single function to recieve all the events.
textBox1.TextChanged += new System.EventHandler(this.txtChanged);
textBox2.TextChanged += new System.EventHandler(this.txtChanged);
textBoxn.TextChanged += new System.EventHandler(this.txtChanged);
And you can use two event types to know the text  before and later the text has changed. You can use this event to know the text whe it has changed.

With keyPress you can know the text of the textbox that is going to change and the new text with:

private void txtChanged(object sender, System.EventArgs e)
      TextBox txtBx = (TextBox) sender;
      string s = txtBx.Text;

It´s true that you must save the text of the textboxes to know where it has changed.
I hope this is interesting for you.
TextChanged is an event that you can assign an event handler to, but I don't think that's quite what you want. What I would do is store the initial value in a variable, then when a button is clicked or the form is closed (I'm not sure if this is in WinForms or ASP.NET), check the new value against the original value and update the database accordingly.
you mean you want to save into the database only if the value in the text box has changed from the intial value when user clicks save or soemthing or winforms
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Update database on a text change event is not a good idea. Just imagine that you are typing 5 letter word. The text change event will fire 5 times. Are going to update db 5 times? I would create a separate button to update db or on the textbox-leave event
animated405Author Commented:

Thanks for the replies. This is ASP.NET, using C#. What I have is a cart where the customer makes the changes to the qty field and then clicks and update button which fires and event that updates each sku with the qty regardless of whether or not the value has changed. This is obvioulsy not the best practive for such an event. I know I could store the values and reference them when the page is posted back to see if they've changed, however somehow I thought that the control knows whehter or not the value is different from when it was originally populated when the page has been posted back.

If I have to go the route of holding onto this information, storing it in the session immediately comes to mind and I could kill it when I'm finished. I'm using a datagrid to display the items, I could loop through it and check against the same information in the array then only update if a change has been made. Just seems like there is a cleaner way than this...

thanks again
Why dont you try with keypress event??

txbox.KeyPress += new...

You can now wich is the key that has been pressed, and the new text.
animated405Author Commented:

thanks atxondo. Wouldn't that imply that an event handler would get fired as soon as that action occurs, on each textbox? what I have is several textboxes on a form that when a button is clicked then go through them all and if the value has changed submit to the database.

thanks again
All Courses

From novice to tech pro — start learning today.