VB.NET What is Sender used for?

前端 未结 3 602
不知归路
不知归路 2020-12-18 00:41

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 S         


        
3条回答
  •  Happy的楠姐
    2020-12-18 00:56

    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
    

提交回复
热议问题