CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    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

  2. #2
    Join Date
    May 2002
    Posts
    511

    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
  •  





Click Here to Expand Forum to Full Width

Featured