Home All Groups Group Topic Archive Search About

What is the best approach to determine if there is a change?

Author
15 Oct 2007 9:54 PM
Andrew
Hello, friends,

We are developing c#.net Windows app using .net 2005. In a form, there are
about 80 editable controls, such as text boxes, combo boxes, masket boxes,
etc.

We need to detect if any of their values has been changed when a user
decides to close this window. If yes, we need to prompt a message and save
accordingly.

Of course, we can write 80 ValueChanged() event handler for each control to
monitor its value. But, do we have a better way to do this?

Thanks a lot.

Author
15 Oct 2007 10:03 PM
Chris Mullins [MVP - C#]
"Andrew" <And***@discussions.microsoft.com> wrote:
> We are developing c#.net Windows app using .net 2005. In a form, there are
> about 80 editable controls, such as text boxes, combo boxes, masket boxes,
> etc.
>
> We need to detect if any of their values has been changed when a user
> decides to close this window. If yes, we need to prompt a message and save
> accordingly.
>
> Of course, we can write 80 ValueChanged() event handler for each control
> to
> monitor its value. But, do we have a better way to do this?

In the page load event, I would rip through the controls, and add in a
changed handler. Your code will end up looking like:

foreach(control c in this.controls)
{
    if (typeof(c) is textBox)
        ((TextBox)c).TextChanged += TextChangedHandler(...);
    if (typeof(c) is listbox)
        ((ListBox)c).SelectedIndexChanged += LBChangedHandler(...);
}

Another option is to databind all your fields/controls, then let the dataset
tell you that things have changed.

--
Chris Mullins
Author
15 Oct 2007 10:34 PM
Andrew
Thanks a lot, but databind is not an option since our data are
populated/saved through web services.

Show quote
"Chris Mullins [MVP - C#]" wrote:

> "Andrew" <And***@discussions.microsoft.com> wrote:
> > We are developing c#.net Windows app using .net 2005. In a form, there are
> > about 80 editable controls, such as text boxes, combo boxes, masket boxes,
> > etc.
> >
> > We need to detect if any of their values has been changed when a user
> > decides to close this window. If yes, we need to prompt a message and save
> > accordingly.
> >
> > Of course, we can write 80 ValueChanged() event handler for each control
> > to
> > monitor its value. But, do we have a better way to do this?
>
> In the page load event, I would rip through the controls, and add in a
> changed handler. Your code will end up looking like:
>
> foreach(control c in this.controls)
> {
>     if (typeof(c) is textBox)
>         ((TextBox)c).TextChanged += TextChangedHandler(...);
>     if (typeof(c) is listbox)
>         ((ListBox)c).SelectedIndexChanged += LBChangedHandler(...);
> }
>
> Another option is to databind all your fields/controls, then let the dataset
> tell you that things have changed.
>
> --
> Chris Mullins
>
>
>
Author
15 Oct 2007 10:51 PM
Chris Mullins [MVP - C#]
You can't databind against a web service? That's news to me.

I guess I'll have to stop shipping those applications that do that.
Seriously though, I've seen it done with both ASP.Net and WinForms
applications. It may not be suitable for your app, but it's certainly
possible.

http://channel9.msdn.com/Showpost.aspx?postid=200444

--
Chris Mullins


Show quote
"Andrew" <And***@discussions.microsoft.com> wrote in message
news:AD61429B-7E69-4CD0-905D-E948FFDEAB51@microsoft.com...
> Thanks a lot, but databind is not an option since our data are
> populated/saved through web services.
>
> "Chris Mullins [MVP - C#]" wrote:
>
>> "Andrew" <And***@discussions.microsoft.com> wrote:
>> > We are developing c#.net Windows app using .net 2005. In a form, there
>> > are
>> > about 80 editable controls, such as text boxes, combo boxes, masket
>> > boxes,
>> > etc.
>> >
>> > We need to detect if any of their values has been changed when a user
>> > decides to close this window. If yes, we need to prompt a message and
>> > save
>> > accordingly.
>> >
>> > Of course, we can write 80 ValueChanged() event handler for each
>> > control
>> > to
>> > monitor its value. But, do we have a better way to do this?
>>
>> In the page load event, I would rip through the controls, and add in a
>> changed handler. Your code will end up looking like:
>>
>> foreach(control c in this.controls)
>> {
>>     if (typeof(c) is textBox)
>>         ((TextBox)c).TextChanged += TextChangedHandler(...);
>>     if (typeof(c) is listbox)
>>         ((ListBox)c).SelectedIndexChanged += LBChangedHandler(...);
>> }
>>
>> Another option is to databind all your fields/controls, then let the
>> dataset
>> tell you that things have changed.
>>
>> --
>> Chris Mullins
>>
>>
>>
Author
16 Oct 2007 10:40 AM
James Crosswell
Andrew wrote:
> Thanks a lot, but databind is not an option since our data are
> populated/saved through web services.

You can databind against pretty much anything... If you're fetching the
data from a web service then you must be storing it locally in some kind
of class and should be able to bind to the properties of that class.

None the less, the other solution proposed should work fine as well if
you don't like data binding.

Best Regards,

James Crosswell
Microforge.net LLC
http://www.microforge.net

AddThis Social Bookmark Button