什么是事件参数“sender"?和“e" [英] What are the event arguments "sender" and "e"

查看:54
本文介绍了什么是事件参数“sender"?和“e"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我想要调用的事件:

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

调用方法如下:

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?

推荐答案

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

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

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

The sender and e arguments are the standard signature of event handlers. Sender is the object that raised the event and e contains the data of the event. All events in .NET contain such arguments.

EventArgs 是所有事件参数的基类,对事件的描述不多.几个事件使用派生类来提供更多数据,例如.KeyPress 事件使用包含实际键的 KeyEventArgs 类按下其 KeyChar 属性.

EventArgs is 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 KeyPress event uses the KeyEventArgs class which contains the actual key pressed in its KeyChar property.

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

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

这篇关于什么是事件参数“sender"?和“e"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆