VB.NET What is Sender used for?
Asked Answered
L

3

7

I'm confused as to the purpose of the sender parameter in Winform controls, for example:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

I understand i can verify what sender holds by doing something as so:

If TypeOf sender Is Label Then
 'Execute some code...
End If

But is there a good reason that sender is included in every single control when it generates the sub-routine for me? In other words, i double click on a Form and i get the Private Sub form_load (sender....) and e As System.EventArgs.

What are some common usage of these two parameters? Are they always required?

Letishaletitia answered 29/7, 2012 at 21:49 Comment(0)
P
20

sender contains the sender of the event, so if you had one method bound to multiple controls, you can distinguish them.

For example, if you had ten buttons and wanted to change their text to "You clicked me!" when you clicked one of them, you could use one separate handler for each one using a different button name each time, but it would be much better to handle all of them at once:

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
    DirectCast(sender, Button).Text = "You clicked me!"
End Sub
Playback answered 29/7, 2012 at 21:52 Comment(3)
Thank you very much minitech, very quick response! Why are you using DirectCast to convert the incoming (sender) to a button, can't we assume its a button since your Handles reflect Button1.Click all way up to Button9.Click? Or is this referring to something else that i missed ;) Thanks!Letishaletitia
@Intrus: I'm using DirectCast because sender is declared as an Object (that's just the way event handlers work) and I can assume it's a Button, so that's why I'm casting it without checks. If you code without Option Strict On, then it will compile using sender.Text, but it will be slower, and finding errors will be more difficult later. (ProTip: Always turn Option Strict On! It's in your settings.)Playback
I've become accustom of Option Strict On, but thank you for the tip and the very clear answer.Letishaletitia
D
5

e refers to the event arguments for the used event, they usually come in the form of properties/functions/methods that get to be available on it.

In this example the label text property will contain the BorderColor set for the footer style of our GridView when its FooterRow, determined from the row sent as a property on the event arguments parameter, binds the data with the GridView DataSource.

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.Footer Then
            lblFooterColor.Text = e.Row.Style("BorderColor")
        End If
End Sub
Disappearance answered 29/7, 2012 at 22:29 Comment(1)
Great answer to my 2nd half of the question CoderRoller, thank you very much!Letishaletitia
M
2

For the first half of the question:

sender is used when the callback handles multiple events, to know which object did fire the event.

For example, instead of cut-and-paste the same code in two callback functions, you can have the same code managing two different button click events:

Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
   Dim s As String
   If sender Is Button1 Then
      s = "button1"
   ElseIf sender Is Button2 Then
      s = "button2"
   End If
   MessageBox.Show("You pressed: " + s)
End Sub

Reference here.

Microcopy answered 14/1, 2016 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.