vb.net 什么是事件参数“sender”和“e”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13929529/
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:30:23  来源:igfitidea点击:

What are the event arguments "sender" and "e"

vb.net

提问by whytheq

I'm struggling to understand these arguments.
They are automatically generated and I don't need to give them much thought but if I want to call one of these events from some other part of the form then I need to supply these arguments with values - not easy as I'm still unsure what they mean.

我正在努力理解这些论点。
它们是自动生成的,我不需要考虑太多,但是如果我想从表单的其他部分调用这些事件之一,那么我需要为这些参数提供值 - 这并不容易,因为我仍然不确定他们的意思。

Here is the event I'd like to Call:

这是我想要的事件Call

Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
    Dim outString As String
    Select Case stringTextBox.Text
        Case ""
            outString = "You entered an empty string"
        Case "10"
            outString = "10"
        Case Else
            outString = "Your string is not covered by the cases"
    End Select
    strResultTextBox.Text = outString
End Sub

Here is the calling method:

下面是调用方法:

Private Sub stringTextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles stringTextBox.KeyDown
    If e.KeyCode = Keys.Enter Then
        Call stringButton_Click(Me, Me) '<<(Me,Me) is my incorrect attempt
    End If
End Sub

What arguments should I use and why?

我应该使用什么参数,为什么?

回答by Panagiotis Kanavos

DON'T call event handlers from other parts of your code! This is very sloppy coding. Create a separate method that does what you need, with the parameters you need, and call it both from the event handler and any other part of your form, eg

不要从代码的其他部分调用事件处理程序!这是非常草率的编码。创建一个单独的方法来执行您需要的操作,使用您需要的参数,并从事件处理程序和表单的任何其他部分调用它,例如

Private Function String GetResponseMessage(ByVal input as String)
    Select Case input
        Case ""
            Return "You entered an empty string"
        Case "10"
            Return "10"
        Case Else
            Return "Your string is not covered by the cases"
    End Select
End Function

Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
    Dim outString As String=GetResponseMessage(stringTextBox.Text)
    strResultTextBox.Text = outString
End Sub

The senderand earguments are the standard signature of event handlers. Sender is the object that raised the event and econtains the data of the event. All events in .NET contain such arguments.

发件人Ë参数是事件处理程序的标准签名。Sender 是引发事件的对象,e包含事件的数据。.NET 中的所有事件都包含此类参数。

EventArgsis the base class of all event arguments and doesn't say much about the event. Several events use a derived class to supply more data, eg. a KeyPressevent uses the KeyEventArgs class which contains the actual key pressed in its KeyCharproperty.

EventArgs是所有事件参数的基类,对事件的描述不多。一些事件使用派生类来提供更多数据,例如。一个按键响应事件使用KeyEventArgs类,其中包含在其按实际键KeyChar财产。

This is basic .NET programming stuff and you should spend some time understanding .NET events and Windows Forms instead of trusting auto-generated code

这是基本的 .NET 编程内容,您应该花一些时间了解 .NET 事件和 Windows 窗体,而不是相信自动生成的代码

回答by major-mann

Sender is used to specify which object generated the event, e is the event data itself. For example, usually when you click on a button in a form "sender" would be the button, and e would be the mouse event.

Sender 用于指定哪个对象产生了事件,e 是事件数据本身。例如,通常当您单击表单中的按钮时,“发件人”将是按钮,而 e 将是鼠标事件。

In you case you could simply change

在你的情况下,你可以简单地改变

Call stringButton_Click(Me, Me)

To

Call stringButton_Click(Nothing, EventArgs.Empty)

回答by syed mohsin

You can do this by making a new method. Do not call the event handler.

您可以通过创建一个新方法来做到这一点。不要调用事件处理程序。

Public sub abcd()
Dim outString As String
Select Case stringTextBox.Text
    Case ""
        outString = "You entered an empty string"
    Case "10"
        outString = "10"
    Case Else
        outString = "Your string is not covered by the cases"
End Select
strResultTextBox.Text = outString
end sub

Call this method from where you want

从你想要的地方调用这个方法

Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
abcd()
End Sub

and from this

并由此

Private Sub stringTextBox_KeyDown(ByVal sender As System.Object, ByVal e As 
 System.Windows.Forms.KeyEventArgs) Handles stringTextBox.KeyDown
If e.KeyCode = Keys.Enter Then
    abcd()
End If
End Sub

回答by user6218508

It's better to move your code into a separate function, as already spoken above, but here's a workaround: call stringButton_Click(Nothing,Nothing)

如上所述,最好将您的代码移动到一个单独的函数中,但这里有一个解决方法: call stringButton_Click(Nothing,Nothing)