I'm creating checkboxes dynamically and adding them to a form.
Then I use a delegate to add events to the checkbox. (I have a function to handle the .click event of the checkboxes)
But, I can't tell which check box was clicked. How can I tell which one is clicked. I thought I could use the 'sender' object but it isn't working.
Here's my code:
In the MyDelegateFunction(), I need to know which checkbox sent the .click message to handle. How can I know which checkbox was clicked?Code:// The delegate public delegate void MyEventHandler(EventArgs e); // Adding controls to the form and adding a handler for the .click event private void Form1_Load(object sender, EventArgs e) { CheckBox c = new CheckBox(); c.Text = "yadda"; c.Tag = "ONE"; c.Location = new Point(20, 20); this.Controls.Add(c); c.Click += new System.EventHandler(MyDelegateFunction); c = new CheckBox(); c.Text = "yadda"; c.Tag = "TWO"; c.Location = new Point(40, 40); this.Controls.Add(c); c.Click += new System.EventHandler(MyDelegateFunction); } // The function to handle all checkboxes .click event public void MyDelegateFunction(Object sender, EventArgs e) { MessageBox.Show("raised..." + sender.ToString() + " " + e.ToString()); MessageBox.Show(sender.GetType().ToString()); }![]()
Thanks.





Reply With Quote