473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 29788
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jbsound | last post by:
I have been banging my head against this one and I'm sure I'm missing something: Got a WinForms app in VB.NET with controls bound to a dataset. For the sake of this discussion, let's use just...
2
by: VMI | last post by:
I'm trying to some dynamic searching to my windows Form so I added code in my TextBox.TextChanged so that, after every text change, a search is done to a datatable. So if I want to type the string...
2
by: jorge | last post by:
Hello I have the following situation: (everything is dynamic (controls.add)) 1. Button.Init { WasButtonClickFired = true } 2. TextBox.TextChanged { WasButtonClickFired?
1
by: Spencer H. Prue | last post by:
I have a textbox and a button on my asp.net form. I have some Smtp code in my button click procedure. When the user hits the return for the textbox the button procedure runs. When the button is...
2
by: Fabio Cannizzo | last post by:
Hi. The content of my textbox is "ABCD". If I select the letter 'C' and I press 'X', the content obviously changes to "ABXD". Thsi however generates two separate "TextChanged" events 1) ...
2
by: pvdg42 | last post by:
Working on a web project (academic example) for a beginning VB class where there are four text boxes, each to hold four characters. I want focus to shift automatically to the next text box when the...
5
by: S_K | last post by:
This is a weird problem.. I have a page with a textbox and a button (along with a bunch of other stuff). Both the textbox and the button have their SEPERATE events defined for the textchanged...
0
by: Jason Huang | last post by:
Hi, In my C# 1.1 Windows form project, I have a TextBox TextChanged for TextBox1 on the Form1. How do I limit the TextChanged event to only real user doing something on that TextBox? I mean...
9
by: David C | last post by:
I have an asp.net page that contains a FormView with several controls bound to a SqlDataSource. One of the controls is a TextBox bound to a date column. I have the following additional properties...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.