473,789 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can a parent know if a child control chages

We are building a framework in C#. One thing that we would like to do
is know when any child control (checkboxes, radio buttons, etc.) is
changed in a container of controls (such as a panel or a form). This
seems like it ought to be simple, but we cannot figure out how to do it
in .NET without writing code in each object.

Bill Zack

Nov 17 '05 #1
6 1378
Bill,

You can use reflection to iterate over the events exposed by any
control, and then set up the appropriate event handlers (you can even set up
one generic event handler to handle ALL events in C# 2.0).

If you need specific event handlers, then you can set them up according
to the event you are listening on, and attach any number of like controls to
it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ca*******@caspe rshouse.com

"wzack" <wz***@compuser ve.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
We are building a framework in C#. One thing that we would like to do
is know when any child control (checkboxes, radio buttons, etc.) is
changed in a container of controls (such as a panel or a form). This
seems like it ought to be simple, but we cannot figure out how to do it
in .NET without writing code in each object.

Bill Zack

Nov 17 '05 #2
That's what things like OnChange() are for, and as far as i know, you
have to do it to every control.

bool unchanged = true;

private void checkBox1_Check edChanged(objec t sender, EventArgs e) {
unchanged = false;
}

that's probably how I'd do it. i'm new to C# tho. I'd do that for
every control on the form, by creating an updated() function that I
could call.

wzack wrote:
We are building a framework in C#. One thing that we would like to do
is know when any child control (checkboxes, radio buttons, etc.) is
changed in a container of controls (such as a panel or a form). This
seems like it ought to be simple, but we cannot figure out how to do it
in .NET without writing code in each object.

Bill Zack

Nov 17 '05 #3
As someone stated on another forum, what I am looking for is a "Form IsDirty"
event. Any other ideas?

Thanks
Bill Zack
"jeremiah johnson" wrote:
That's what things like OnChange() are for, and as far as i know, you
have to do it to every control.

bool unchanged = true;

private void checkBox1_Check edChanged(objec t sender, EventArgs e) {
unchanged = false;
}

that's probably how I'd do it. i'm new to C# tho. I'd do that for
every control on the form, by creating an updated() function that I
could call.

wzack wrote:
We are building a framework in C#. One thing that we would like to do
is know when any child control (checkboxes, radio buttons, etc.) is
changed in a container of controls (such as a panel or a form). This
seems like it ought to be simple, but we cannot figure out how to do it
in .NET without writing code in each object.

Bill Zack

Nov 17 '05 #4
I know that this isn't what you asked... :) ...but I'm wondering: why
aren't you doing this in a model object, rather than in the form
itself? That's what I'm doing: underneath each form I have a
corresponding model, and the model tracks differences from the original
values, and informs the form (for example) whether to enable the Save
button.

Nov 17 '05 #5
Another (ugly) way would be to write your own (derived) version of each
type of control that implements an interface that defines properties
and methods to track changes: ResetDirty(), and IsDirty.

public interface ITracksChanges
{
void SetOriginalValu e(object val);
bool IsDirty { get; }
}

Each derived control could then store its original value and compare
that against its current value. Then you use the derived controls in
all of your projects.

For the record, I dislike this solution. It's an ugly hack on the
WinForms framework, and using view / model would be a far better way to
go. I'm just offering it as a (hack) alternative.

Nov 17 '05 #6
Is there any reason why I could not use reflection at startup to walk down
the control hierarchy and set a common handler for any events that I am
interested in for that control type?

"Bruce Wood" wrote:
Another (ugly) way would be to write your own (derived) version of each
type of control that implements an interface that defines properties
and methods to track changes: ResetDirty(), and IsDirty.

public interface ITracksChanges
{
void SetOriginalValu e(object val);
bool IsDirty { get; }
}

Each derived control could then store its original value and compare
that against its current value. Then you use the derived controls in
all of your projects.

For the record, I dislike this solution. It's an ugly hack on the
WinForms framework, and using view / model would be a far better way to
go. I'm just offering it as a (hack) alternative.

Nov 17 '05 #7

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

Similar topics

25
6213
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I rarely refer to a subform ir parent form's controls or records from the other form. Instead, I call a procedure in that form that does the job. That's all fine and good, and nothing at all revolutionary, but it leaves one with an annoying...
13
7412
by: Stuart McGraw | last post by:
I haven't been able to figure this out and would appreciate some help... I have two tables, both with autonumber primary keys, and linked in a conventional master-child relationship. I've created forms for both those tables, and inserted the child table form into the master table form as a subform. It works just as it is supposed to, in that I can create a new master record, and then add detail records.
1
11588
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution. ...
5
2358
by: Patrick Vanden Driessche | last post by:
Hi All, I'm currently writing an in-house Form validation framework (WinForms) which is based on 'Component'-inheriting object. So basically, I have a small hierarchy. FormValidator +-- ControlValidator +--ValidationRule
10
4030
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
4
3565
by: Richard Lewis Haggard | last post by:
What is the mechanism by which a child window can notify its parent that it has been clicked on? -- Richard Lewis Haggard www.Haggard-And-Associates.com
8
5984
by: Rick | last post by:
VS 2005 I' m setting up a parent/child datagridviews in a form. I am doing a lot of this by hand coding in order to get the feel of things. I want a change in the parent table to trigger a change in the child. Where is the best place (if I cannot set it up to be automatic through relationships) to catch the change in the parent to refetch the child rows?
1
1571
by: Sebouh | last post by:
Hello guys. This time, i need to implement waitpid in order to know if one of the children of a parent has finished executing. The assignment requires me to run a limited number of child processes. Then if max is reached and one more is asked to run by the user from the command line, the parent must wait and see if there is a free spot. (in the array of children) Now i know i have to use waitpid somewhere, but using it as: wait child1,...
4
6058
by: Harlequin | last post by:
I have a question concerning the need to trigger events within a "child" subform which is itself enbedded within a master "parent" form and which is accessible via a tab in the parent form. Becuase this is all very difficult to explain in words, please bear with me as I endevour to explain what it is I am trying to do. It would be helpful if I could attach a graphics file to this posting that would help explain what it is I'm trying to achieve...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.