469,290 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

C# TextBox TextChanged problem

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
Jan 22 '06 #1
4 29548
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 22 '06 #2
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.
"Jason Huang" <Ja************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Jan 22 '06 #3
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

"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fi> wrote in message
news:eq**************@tk2msftngp13.phx.gbl...
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Jan 22 '06 #4
On Sun, 22 Jan 2006 13:20:31 +0800, "Jason Huang"
<Ja************@hotmail.com> wrote:
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

--

The ultimate truth is that there is no ultimate truth
Jan 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by VMI | last post: by
2 posts views Thread by Fabio Cannizzo | last post: by
2 posts views Thread by pvdg42 | last post: by
reply views Thread by Jason Huang | last post: by
9 posts views Thread by David C | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.