Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


checking text box for textchanged

Posted on 2004-10-27
Medium Priority
Last Modified: 2012-08-14

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!!
Question by:animated405
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

ID: 12429008
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.

Expert Comment

ID: 12430447
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

asp.net or winforms
LVL 28

Expert Comment

ID: 12432973
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


Author Comment

ID: 12440752

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

Expert Comment

ID: 12473042
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.

Author Comment

ID: 12474765

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

Accepted Solution

atxondo earned 500 total points
ID: 12482819
 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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question