VB.NET 发件人是干什么用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11713250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:14:00  来源:igfitidea点击:

VB.NET What is Sender used for?

vb.netsender

提问by Dayan

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

我对senderWinform 控件中参数的用途感到困惑,例如:

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

End Sub

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

我知道我可以sender通过这样做来验证什么是有效的:

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....)ande As System.EventArgs.

但是,当它为我生成子例程时,是否有充分的理由将发送方包含在每个控件中?换句话说,我双击一个表单,然后我得到了Private Sub form_load (sender....)ande As System.EventArgs。

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

这两个参数有哪些常见用法?他们总是需要吗?

Thank you,

谢谢,

Dayan D.

达扬 D.

回答by Ry-

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

sender包含事件的发送者,因此如果您将一个方法绑定到多个控件,则可以区分它们。

For example, if you had ten buttons and wanted to change their text to "You clicked me!" when you clicked one of them, you coulduse 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

回答by CoderRoller

erefers 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.

e指的是所用事件的事件参数,它们通常以属性/函数/方法的形式出现,可以在其上使用。

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

在此示例中,标签文本属性将包含为我们的页脚样式设置的 BorderColor,GridView当它的 FooterRow(根据作为事件参数参数上的属性发送的行确定)将数据与 GridView 数据源绑定时。

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

回答by Zac

For the first half of the question:

对于问题的前半部分:

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

sender当回调处理多个事件时使用,以了解哪个对象确实触发了事件。

For example, instead of cut-and-pastethe 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.

参考这里