Solved

checking text box for textchanged

Posted on 2004-10-27
437 Views
Last Modified: 2012-08-14

Hello,
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!!
0
Question by:animated405
    7 Comments
     
    LVL 15

    Expert Comment

    by:Timbo87
    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.
    0
     
    LVL 3

    Expert Comment

    by:sajay
    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
    0
     
    LVL 28

    Expert Comment

    by:iboutchkine
    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
    0
     

    Author Comment

    by:animated405

    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
    0
     
    LVL 3

    Expert Comment

    by:atxondo
    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.
    0
     

    Author Comment

    by:animated405

    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
    0
     
    LVL 3

    Accepted Solution

    by:
    Hi,
     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.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    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…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now