Results 1 to 5 of 5

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

  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.

  2. #2
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: Convert sender object to a control?

    Personally, if I know that the sender is of type CheckBox then I would type cast the sender as a checkbox and check the name proprty to see which one it is. IE. ((CheckBox)sender).Name.
    Currently Using: VS 2005 Professional

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Convert sender object to a control?

    Since you haven't set the name of your controls, you could use Tag to identify your sender.

    Code:
    ((CheckBox)sender).Tag.ToString()
    Show Appreciation. Rate Posts.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Re: Convert sender object to a control?

    Thanks guys.

    I was trying to convert the sender to an object when I was trying to cast it.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Another way would be:
    c# Code:
    1. CheckBox cbx = sender as CheckBox;
    2.  
    3. if (cbx != null)
    4. {
    5.     // Use cbx here.
    6. }
    The 'as' operator will attempt to cast the reference as the specified type and return null if it fails, where an explicit cast will throw an exception if it fails. It's better if there can be any doubt about the type of the object being cast. It is a good way to create more robust code.
    Last edited by jmcilhinney; Mar 11th, 2007 at 03:41 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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