Vb.Net Sender和e事件参数

本文对比了VB6和VB.NET中事件子例程的区别,详细解释了VB.NET中Handles子句的作用以及如何利用sender和e参数进行更灵活的事件处理。通过具体示例,展示了如何使用这些参数简化代码,提高程序的可维护性和扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In VB6, an event subroutine, like Button1_Click, was much less complicated because the system called the subroutine strictly by name. If a Button1_Click event existed, the system called it. It's direct and straightforward.

在VB6中,事件子例程(如Button1_Click)不那么复杂,因为系统严格按名称调用该子例程。 如果存在Button1_Click事件,则系统将其调用。 它是直接而直接的。

But in VB.NET, there are two major upgrades that make VB.NET SOOPercharged (that's "OOP" for Object Oriented Programming).

但是在VB.NET中,有两个主要的升级使VB.NET SOOPercharged(即面向对象编程的“ OOP”)。

  1. The "Handles" clause controls whether the system calls the subroutine, not the name.

    “句柄”子句控制系统是否调用子例程,而不是名称。
  2. The sender and e parameters are passed to the subroutine.

    sender和e参数传递到子例程。

参数的使用 ( Use of Parameters )

Let's look at a simple example to see the difference that parameters make in VB.NET.

让我们来看一个简单的示例,以了解参数在VB.NET中的区别。

Private Sub Button1_Click( 
 ByVal sender As System.Object,
 ByVal e As System.EventArgs
 ) Handles Button1.Click
 ' Your code goes here
End Sub

Event subroutines always receive a "sender" object and a system EventArgs parameter "e". Because the EventArgs parameter is an object, it supports whatever properties and methods are necessary. For example, the old VB6 MouseMove event subroutine used to receive four parameters:

事件子例程始终接收“发送者”对象和系统EventArgs参数“ e”。 因为EventArgs参数是一个对象,所以它支持所需的任何属性和方法。 例如,旧的VB6 MouseMove事件子例程用于接收四个参数:

  • Button As Integer

    按钮为整数
  • Shift As Integer

    整数移位
  • X As Single

    X单身
  • Y As Single

    Y为单身

When more advanced mice came out with more buttons, VB6 had a real problem supporting them. VB.NET only passes one MouseEventArgs parameter but it supports a lot more properties and methods. And each of them are objects that support even more. For example, the e.Button property contains all these properties:

当更多高级鼠标带有更多按钮时,VB6确实在支持它们方面遇到了问题。 VB.NET仅传递一个MouseEventArgs参数,但它支持更多的属性和方法。 每个对象都是支持更多对象的对象。 例如,e.Button属性包含所有这些属性:

  • Left

    剩下
  • Middle

    中间
  • Right

  • None

    没有
  • XButton1

    XButton1
  • XButton2

    XButton2

If someone invents a "trancendental" mouse with a "virtual" button, VB.NET will only have to update the .NET Framework to support it and no previous code will break as a result.

如果有人发明了带有“虚拟”按钮的“超然”鼠标,则VB.NET只需更新.NET Framework即可支持它,并且以前的代码也不会因此而中断。

There are a number of .NET technologies that absolutely depend on these parameters. For example, since your PC usually only has a single screen to display graphics, your code has to merge the graphics it creates into the same image used by Windows. For that reason, a single "graphics" object has to be shared. The major way that your code is able to use that "graphics" object is to use the e parameter that is passed to the OnPaint event with the PaintEventArgs object.

有许多.NET技术完全依赖这些参数。 例如,由于您的PC通常只有一个屏幕来显示图形,因此您的代码必须将其创建的图形合并到Windows使用的同一图像中。 因此,必须共享一个“图形”对象。 您的代码能够使用“图形”对象的主要方式是使用与PaintEventArgs对象一起传递给OnPaint事件的e参数。

Protected Overrides Sub OnPaint(
 ByVal e As System.Windows.Forms.PaintEventArgs)
 Dim g As Graphics = e.Graphics

其他例子 ( Other Examples )

What else can you do with these parameters? To illustrate, suppose you want to find whether a string, perhaps something you entered into a Textbox, exists in any one of a collection of other Textboxes when you click on one. You could code a few dozen virtually identical subroutines for each Textbox:

您还可以使用这些参数做什么? 为了说明这一点,假设您要查找一个字符串,也许是您输入到文本框中的内容在单击其他文本框的集合中的任何一个时是否存在。 您可以为每个文本框编写几十个几乎相同的子例程:

If TextBox42.Text.IndexOf(
 SearchString.Text) = -1 
 Then NotFound.Text = 
 "Not Found"

But it's a lot easier to code just one and let it handle all of them. The sender parameter will reveal which Textbox was clicked.

但是只编写一个代码并让其处理所有代码要容易得多。 sender参数将显示单击了哪个文本框。

Private Sub FindIt(
 ByVal sender As System.Object,
 ByVal e As System.EventArgs
 ) Handles TextBox1.Enter, 
 TextBox2.Enter, 
 . . . and on and on . . .
 TextBox42.Enter
 Dim myTextbox As TextBox
 myTextbox = sender
 Dim IndexChar As Integer = 
 myTextbox.Text.IndexOf( 
 SearchString.Text)
 If IndexChar = -1 Then _
 NotFound.Text = "Not Found" _
 Else _
 NotFound.Text = "Found It!"
 End Sub

Recently, a programmer asked me for a better way to "delete the line that was clicked in any of six specified lists." He had it working in a couple of dozen lines of code that simply confused me. But using sender, it was really quite simple:

最近,一个程序员问我一种更好的方法来“删除在六个指定列表中的任何一个中单击的行”。 他使用了几十行代码,使我感到困惑。 但是使用发件人,确实非常简单:

Private Sub ListBox_Click( 
 ByVal sender As Object, 
 ByVal e As System.EventArgs
 ) Handles ListBox1.Click, ListBox2.Click
 Dim myListBox As New ListBox
 myListBox = sender
 myListBox.Items.RemoveAt(myListBox.SelectedIndex)
End Sub

One more example to nail down the point is a question that was sent in by Pierre in Belgium. Pierre was testing the equality of Button1 and sender using the Is operator for objects:

比利时的皮埃尔(Pierre)提出了一个进一步确定这一点的例子。 Pierre正在使用Is运算符测试对象的Button1和发送方是否相等:

If sender Is Button1 Then ...

This is syntactically correct because sender and Button1 are both objects that can be referenced. And since sender really is identical with Button1, why doesn't it work?

在语法上这是正确的,因为sender和Button1都是可以引用的对象。 而且由于发件人确实与Button1相同,所以为什么不起作用?

The answer depends on a keyword that is found a little earlier in the statement. First, let's check the Microsoft documentation for the Is operator.

答案取决于在语句中较早找到的关键字。 首先,让我们检查Is操作符的Microsoft文档。

Visual Basic compares two object reference variables with the Is Operator. This operator determines if two reference variables refer to the same object instance.

Visual Basic将两个对象引用变量与Is运算符进行比较。 该运算符确定两个引用变量是否引用同一对象实例。

Notice that sender is passed ByVal. That means that a copy of Button1 is passed, not the actual object itself. So when Pierre tests to see if sender and Button1 are the same instance, the result is False.

请注意,发件人已通过ByVal传递。 这意味着将传递Button1的副本,而不是实际对象本身。 因此,当Pierre测试以查看sender和Button1是否为同一实例时,结果为False。

To test whether Button1 or Button2 has been clicked, you have to turn sender into an actual Button object and then test a property of that object. Text is usually used, but you could test a value in Tag or even the Location property.

要测试是否单击了Button1或Button2,必须将发件人变成一个实际的Button对象,然后测试该对象的属性。 通常使用文本,但是您可以测试Tag或Location属性中的值。

This code works:

此代码有效:

Dim myButton As Button
myButton = sender
If myButton.Text = "Button1" Then

翻译自: https://www.thoughtco.com/vbnet-sender-and-e-event-parameters-3424242

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值