473,416 Members | 1,557 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,416 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 29790
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.