Results 1 to 5 of 5

Thread: [RESOLVED]Convert sender object to a control?

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Resolved [RESOLVED]Convert sender object to a control?

    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:
    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());
            }
    In the MyDelegateFunction(), I need to know which checkbox sent the .click message to handle. How can I know which checkbox was clicked?

    Thanks.
    Last edited by sooner; Mar 9th, 2007 at 11:45 AM.

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