An apply button on my standard winform is disabled on loading and I want to enable it, if a state of any other control changes (checkboxes, radioboxes, textboxes etc.)
So I could go through the events of every single control and enable the button there but I want to know if there is an easier way to do it, like a global event.
Edit
To clarify: I’m looking for a method where I havn’t to do something with every single control. So that I can add more controls at a later point and don’t have to care about them …
As per your edit I’ve revised my answer to include the functionality you desire.
First you need a generic event handler that should look something like this:
Where button1 is the button you wish to enable.
Then you’re going to need a recursive method to not only iterate through your
Form.Controls, but any container type controls contained in there as well. I’ve included handling of some common container controls as well as some basic data entry controls:The first part of the
if elseblock checks to see if thecontrolis of a type that contains other controls. If it is it then recursively calls the AddEvents method with the newSystem.Windows.Forms.Control.ControlCollectioncontained in thatcontrol.The second part of the
if elseblock checks what type of control c is so that it can be appropriately cast to the correct type, and therefore utilize the correct event. If we are able to determine our control type at this point, the generic event created earlier is added as a handler.Last, you need to call this method. The two best places would probably be either in your constructor or on the
Form.Loadevent. The best place to put it will depend on your specific circumstances. I chose to use my constructor for simplicity, which now looks like this:And that should be all you need to iterate your controls and add the generic event handler. This code is from an actual project I created and I have tested it to ensure it’s proper functionality.
EDIT: I’ve also just tested this using
controls inside of aGroupBoxinside of aPanelinside of aGroupBoxinside of aPanelthat’s on aForm. This is where the usefulness of utilizing recursion comes in. You don’t need to know to exact depth of nesting so long as you properly set up yourif...else...blocks. It will go as deep as it needs to without needing to use nested loops and knowing the exact depth.EDIT2: As a side note, this method could also be used on a more granular level. Let’s say you have multiple
GroupBoxcontrols and you wish to only add the event handler to controls in “grpBox1”. You could callAddEvents(grpBox1.Controls)instead ofAddEvents(this.Controls)and this would only apply the event handler to controls contained ingrpBox1.EDIT3: As onemancat has pointed out in the comments, it’s not entirely necessary to actually check if the control is a
GroupBoxor aPaneletc. because all controls inherit from the base classControlwhich has aControlsproperty. You simply could check if theControlcontains other controls by sayingif (c.Controls.Count > 0) AddEvents(c.Controls);however, in a situation where one wants to choose which container controls to iterate, it would be necessary to check the type as I have in my example. If it’s not necessary to be this granular it does make more sense to check the count and never bother with type checking or casting.