How to check if textbox text changed?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

Besides using a very long if statement and storing old text box values,
is there a way to see if text box values have changed since an event?

For example, say I have 8 textboxes when the form loads. They start
empty. The user types values into 2 of them and clicks search. Before
running the search, I want to make sure I don't waste cpu cycles
because perhaps the user didn't change any values, which means I don't
need to run the search. I can use results already in memory.

Thanks,
Brett
 
Brett,

I would create a control that extends TextBox. In this control, I would
have a reset point which would indicate whether or not the text has changed
from the last reset point.

Then, what you can do is you can set the reset point. When it is set in
your control, you would store the text of the string somewhere in your
control. When you check to see if the control is "dirty", you would then
check the current text against the stored text. If they are different, then
they are dirty.

Of course, this only works if the data source you are comparing against
can be guaranteed to not have changed. The filters could still be the same,
but the data might not be, and the results, different.

Hope this helps.
 
Handle the TextChanged Event of the TextBox, by setting a boolean value
indicating that the text has changed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Hi,
As kevin says, add an eventhandler to TextChanged and trap the change in
a bool for later examination.
---------- snip
private bool boxChanged = false;
.......
this.textBoxTest.TextChanged += new
System.EventHandler(this.textBoxTest_TextChanged);
.......
private void textBoxTest_TextChanged(object sender, System.EventArgs
e)
{
this.boxChanged = true;
}
---------- snop
HTH
Mark
 
Back
Top