C# TextBox TextChanged problem

J

Jason Huang

Hi,

In my C# Windows form MyForm I have some TextBoxes.
In these TextBoxes, we have to detect if the TextChanged event occurs,
if there're changes in these TextBoxes, it will ask if we want to save the
changes when we close the form.
However, when I run the MyForm, it will fetch data from the system and put
into TextBoxes,
and this incurs the textchanged event.
But I am thinking how do I make the TextBox's TextChanged know the
difference between event caused by system and event cuased by user typing?
or are there other events to distinguish this?
Thanks for help.


Jason
 
J

Jani Järvinen [MVP]

Hi Jason,
In my C# Windows form MyForm I have some TextBoxes.
In these TextBoxes, we have to detect if the TextChanged event occurs.
However, when I run the MyForm, it will fetch data from the system and put
into TextBoxes, and this incurs the textchanged event.

The TextChanged event will be fired no matter if it is the user typing text
into the TextBox, or you changing the Text property in code.

Luckily, there's an easy, though not necessarily very elegant solution to
this. You need to declare a (boolean) variable that you set to true before
you change the TextBox's content in code. Then, in the TextChanged event
handler you would simply check this variable, and if it is true, you will
know that it was you who changed the Text property in code.

Another option would be to monitor the KeyPress/KeyDown/KeyUp events or
disengage the TextChanged event handler before updating the TextBox, but I
see these as too difficult/unnecessary actions since a simple helper
variable will simply do.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
P

perspolis

I think u'd better to declare eventhandler dor TextChanged in ur program
not by desinger..
when u fill data after that write eventhandler dor textbox.
 
L

Luc E. Mistiaen

Another simple solution would be to save all your original strings and not
monitor the text changed event at all. At the end, before finishing your
dialog you just check if your control texts are different from the
originals...

/LM
 
R

rossum

Hi,

In my C# Windows form MyForm I have some TextBoxes.
In these TextBoxes, we have to detect if the TextChanged event occurs,
if there're changes in these TextBoxes, it will ask if we want to save the
changes when we close the form.
However, when I run the MyForm, it will fetch data from the system and put
into TextBoxes,
and this incurs the textchanged event.
But I am thinking how do I make the TextBox's TextChanged know the
difference between event caused by system and event cuased by user typing?
or are there other events to distinguish this?
Thanks for help.


Jason
I presume that you have some boolean flags to indicate when each
TextBox has been changed which are set by the TextChanged event for a
box. Whenever a TextBox is loaded from the system you will need to
explicitly reset the flag for that TextBox to false. Initially you
will need to reset all the flags to false in the startup code for your
form.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top