-
July 21st, 2016, 10:40 AM
#1
control arrays and common handler
I have a bunch of CheckBox controls on a Windows form and rather than create a separate CheckChanged event handler for each one I think it'd be more elegant to create an array with them and handle the event in a common handler. But I can't figure out how to create the event handler.
Adding controls to an array seems simple enough, this seems to work...
Code:
public partial class Form2 : Form
{
private CheckBox[] SwitchBCheck;
public Form2()
{
InitializeComponent();
SwitchBCheck = new CheckBox[] {checkBox301,checkBox302,checkBox303};
}
}
But how do I add a common event handler that will respond to any of the check boxes in the array? In VB.NET you can specify what controls are "handled" by an event method by using the "handles" key word.
Thanks
-
July 22nd, 2016, 10:27 AM
#2
Re: control arrays and common handler
Something like this:
Code:
// add event handler
this.checkBox301.CheckedChanged += new System.EventHandler(this.allCheckBoxs_CheckedChanged);
// you can remove it like this
this.checkBox301.CheckedChanged -= new System.EventHandler(this.allCheckBoxs_CheckedChanged);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|