• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

What are the uses of the 'sender' object in WinForms?

What are the uses of the 'sender' object in WinForms?

I just realized I rarely if ever have user the sender object inside an event handler, and can't remember what the sender is.

Please explain who the sender is and how to use that object.

thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 4
  • 3
  • 2
2 Solutions
 
Joel CoehoornDirector of Information TechnologyCommented:
You don't have to let visual studio wire up the events for you.  You can do it yourself, too.  This is especially useful if you want to set up the same or a similar behavior as the response for several different events.  In that case, the 'sender' argument tells you what item was the source of the event.
0
 
Éric MoreauSenior .Net ConsultantCommented:
if the same event handler is used for more then one control, you can use the sender to discover which one triggered the event.
0
 
newbiewebSr. Software EngineerAuthor Commented:
emoreau,

Your explanation made it more clear to me.  I do know how to use the same event handler with more than one control, although I never have.  I now see the use of sender.  if you want unique behavior, you find out the sender and choose the ehavior accordingly.

How do you get this information from sender?  It's an onject so what property do you call to find out which it is?

newbieweb
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
newbiewebSr. Software EngineerAuthor Commented:
My guess is this:

Use "is a" to test what type of object it really is.

Is this true?

newbieweb
0
 
Éric MoreauSenior .Net ConsultantCommented:
see this code:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, Button3.Click
        If sender Is Button2 Then
            MessageBox.Show("You clicked button2")
        ElseIf sender Is Button3 Then
            MessageBox.Show("You clicked button3")
        Else
            MessageBox.Show("I really don't know what you clicked!!!")
        End If

    End Sub
0
 
newbiewebSr. Software EngineerAuthor Commented:
Do you know the syntax for 'is' with C#?
0
 
Joel CoehoornDirector of Information TechnologyCommented:
Another example is if you create a number of similar controls dynamically, rather than in the designer.  In this case, you know that every control that calls the method might be a button.  Then all you have to do is cast the sender as a button and call whatever property you want:



void MyButtonClick(object sender, System.EventArgs e)
{
    Button theButton = (Button)sender;
    theButton.Text = "You clicked me!";
}

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
thanks.

Do you know the C# syntax to check if an object is of a particular type?

newbieweb
0
 
Éric MoreauSenior .Net ConsultantCommented:
private void button1_Click(object sender, EventArgs e)
{
    if (sender == this.button1)
        MessageBox.Show("Button1");
    else
        if (sender == this.button2)
            MessageBox.Show("Button2");
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now