473,387 Members | 1,502 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,387 software developers and data experts.

Handlers for Form Changes

Liz

I have a form with about 25-30 TextBoxes, ComboBoxes, etc; if the user does
not make any changes to the form I don't want to post a "Cancel Changes?"
dialog when they exit the form ... so this is what I'm doing to detect
changes:
// this gets called from the Form_Load event handler

private void SetFormStateChangeHandlers(Control parent) // on 1st
entry, "this" is passed to the method
{
foreach (Control control in parent.Controls)
{
if (control is TextBox || control is ComboBox)
{
control.TextChanged += new
EventHandler(controlStateChanged);
}

// handle container controls which might have child controls
if (control.Controls.Count 0)
{
SetFormStateChangeHandlers(control);
}
}
}

// set the class-level var to be tested on form exit
void controlStateChanged(object sender, EventArgs e)
{
FormHasChanged = true;
}
This *does* work, but there is at least one shortcoming, which is that if
the user changes a control and then undoes the change, I'm not detecting
that ... also, I'm just wondering if there's a better, more straightforward
way of handling the issue ... any other ideas out there?

TIA
Liz

Oct 13 '07 #1
3 3649
Liz wrote:
[...]
This *does* work, but there is at least one shortcoming, which is that if
the user changes a control and then undoes the change, I'm not detecting
that ... also, I'm just wondering if there's a better, more straightforward
way of handling the issue ... any other ideas out there?
Personally, I think the way you're doing it now is actually reasonably
elegant. It's not uncommon at all for a UI to consider any change, even
if it's eventually manually reversed by the user, to be considered a
"change".

That said, it appears to me that you're only actually look at changes to
text fields. So what you're really interested in is whether the text in
the text field is the same.

It seems to me that this would not actually be difficult to implement.
After all of the fields have been initialized, just add all of the
values to a Dictionary<object, stringwhere the control reference is
the key and the Text property value is the value.

Then when you are making the decision as to whether to prompt the user
to discard their changes, run through all the controls again except this
time compare the value in the dictionary with the current value.

If none of the text values are different, then you can safely suppress
the prompt.

Pete
Oct 13 '07 #2
Liz

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Liz wrote:
Personally, I think the way you're doing it now is actually reasonably
elegant. It's not uncommon at all for a UI to consider any change, even
if it's eventually manually reversed by the user, to be considered a
"change".
which probably makes sense; if they "fooled with it" maybe they should
confirm ...
That said, it appears to me that you're only actually look at changes to
text fields. So what you're really interested in is whether the text in
the text field is the same.
actually there are some CheckBoxes my code sample didn't expose in the
interest of keeping it brief... but I guess I could handle those as well; I
was initially concerned about a performance hit; I don't have a code
profiler but I put a crude timer on the event handler fixups and it looks
like they're handled quite quickly .. which makes sense if you stop looking
at code and start thinking about machine instructions instead
It seems to me that this would not actually be difficult to implement.
After all of the fields have been initialized, just add all of the values
to a Dictionary<object, stringwhere the control reference is the key and
the Text property value is the value.

Then when you are making the decision as to whether to prompt the user to
discard their changes, run through all the controls again except this time
compare the value in the dictionary with the current value.
makes sense ... might try that .... thanks, Peter ...

L
Oct 13 '07 #3
Liz wrote:
[...]
actually there are some CheckBoxes my code sample didn't expose in the
interest of keeping it brief... but I guess I could handle those as well; I
was initially concerned about a performance hit; I don't have a code
profiler but I put a crude timer on the event handler fixups and it looks
like they're handled quite quickly .. which makes sense if you stop looking
at code and start thinking about machine instructions instead
Exactly. Compared to all the other work your computer has to do to
handle just one user-input action, the act of calling an event handler
is a minuscule effort. Even if you did that on every single control in
the form.

Of course, if you are saving all the values to start with and then just
checking them at the end, that's even less of a performance hit than the
implementation you've got now (with handling the TextChanged event),
since it only happens once at the beginning and once at the end.

Not that it should really matter either way. I think in this case,
neither method is going to be a problem performance-wise, so you should
just implement the design that you think makes the most sense for your
users.

Pete
Oct 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
4
by: sm | last post by:
Hi, I have a couple of questions with regards to handling errors and exceptions. 1. If I use On Error goto Errhandler ... Errhandler:
9
by: Joseph | last post by:
Hello, Here is my solution below that works but can be improved upon. The improvement that I seek is how to define the handler that handles the txt1 and txt2 controls in an automated fashion. ...
0
by: Michael Maercker | last post by:
Hi! I'm about to go nuts over my serializing problem. This is my situation: I have a Data-Class that can have children of the same class which are stored in a hashtable, i.e: X has A as a...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
2
by: 6tc1 | last post by:
Hi all, I'm inquiring about the mechanism that is used to implement event handlers in .NET (not really relevant, but I'm using C#). Anyway, I've noticed that I can pass an object in an event...
5
by: jrett | last post by:
I'm working on a windows forms application and some of my controls have multiple event handlers assigned to them. I haven't figured out how to add multiple event handlers via the properties...
7
by: Dom | last post by:
In CSharp, can you have multiple handlers for a single event? For example, for a textbox TextChanged event, can I have two separate handlers called up? I have 4 text boxes, and the TextChanged...
1
by: vunet | last post by:
I write a JS library component which is applied to every form on a webpage. The component does something before it submits the form. Now, let's say user has his own onSubmit() handlers returning...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.