?
Solved

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

Posted on 2008-11-13
9
Medium Priority
?
447 Views
Last Modified: 2013-12-17
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
Comment
Question by:newbieweb
  • 4
  • 3
  • 2
9 Comments
 
LVL 18

Assisted Solution

by:Joel Coehoorn
Joel Coehoorn earned 800 total points
ID: 22955355
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
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1200 total points
ID: 22955431
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
 

Author Comment

by:newbieweb
ID: 22955490
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:newbieweb
ID: 22955641
My guess is this:

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

Is this true?

newbieweb
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22955777
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
 

Author Comment

by:newbieweb
ID: 22955813
Do you know the syntax for 'is' with C#?
0
 
LVL 18

Expert Comment

by:Joel Coehoorn
ID: 22956575
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
 

Author Comment

by:newbieweb
ID: 22956680
thanks.

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

newbieweb
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22956822
private void button1_Click(object sender, EventArgs e)
{
    if (sender == this.button1)
        MessageBox.Show("Button1");
    else
        if (sender == this.button2)
            MessageBox.Show("Button2");
}
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question