我需要帮助用简单的英语基本理解,例如:
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
我读到这个:
Private (not to be shared or changed)
Sub (declares a class)
Label1 (is the object)
_ ( I have no idea what the underscore means)
Click ( is the event to trigger the object)
Sender (The user who clicks the object)
As (???? What is this)
Object (object)
, (end of event)
e ( ?? have no idea) AS (??)
EventArgs (?? Not sure)
Handles ( i Would assume that has to do with EventArgs)
Label1.Click (click button).
我已阅读了很多书籍和论坛,我只是不明白,我看到其他人提出问题并得到回复,比如回去看书,我试过了。
我需要一些能够回答这些简单问题的人。当然,我可以复制代码并创建其他人所做的但简单的核心理解,我需要帮助,那里的任何机构都想帮助我,因为这些问题出现了吗?我已经阅读过MSDN论坛,但我再也听不到了。
请帮助我理解。
由于 罗伯特
答案 0 :(得分:3)
这意味着:
Private (this method can only be seen in this class)
Sub (declares a method without a return value)
Label1_Click (is the name of the method - the parts are not significant)
Sender (The object that raised this event)
As (keyword used when declaring the variables and its type)
Object (the type of the sender)
, (separator between parameters)
e (the object that holds the event values)
EventArgs (the type of the event values)
Handles (Declares that this method is an event handler)
Label1 (The object that the event is handled from)
. (The member accessor)
Click (The event that is handled).
答案 1 :(得分:2)
你走在正确的轨道上,但是你在这里看到的某些部分有点过于重要。
Label1_Click是子程序或方法。 Label1_Click只是方法的名称 - 你可以将该行重写为Private Sub Hjahsdfkljnag(sender ...
,它没有任何区别,至少在理论上是这样(尽管我不推荐它)。
这看起来像是在Visual Studio中的UI Builder中创建UI时生成的方法或等效方法 - 当您单击UI中的元素Label1时,它会自动生成。所以Label1_Click是处理Label1点击的方法的描述性名称。自动生成此代码的事实是我不会重命名它的第一个原因 - 其他代码可能已在其他地方生成,并通过此标准化名称引用它。
因此,单步执行:Private
表示该方法仅在当前类中可见/可调用 - 如果您从另一个文件引用该类,则无法查看或使用此方法。 / p>
Sub
是一个子例程,一个包含代码但不返回任何内容的方法 - 这是一种将要一起运行的代码分组的方法。
Label1_Click
是子程序的名称。
(sender As Object, e As EventArgs)
是参数。每次调用该方法时,调用代码必须提供一个名为“sender”的Object(一种非常通用的类型)和一个名为“e”的EventArgs对象(一种更具体的类型,其中包含有关事件的信息)。这些将包含有关发生单击的上下文和发送的参数的信息 - 在这种情况下,可能对您没有多大用处。
Handles Label1.Click
将方法绑定到事件 - 说当有人点击Label1时,此方法应该运行。
答案 2 :(得分:2)
快速解释:
Private (It is accessible from within class, Read "Access Modifiers" for more info)
Sub (Its a sub-routine(does not return value) or function(return value))
Label1 (It is the object for which event handling is going to define)
_ (A convention to separate the `object` and `its event` name)
Click (The name of event)
Sender (1st argument, name of variable, in this case `The user who clicks the object`)
As (Keyword. Works as a bridge to represents the variable and its type) e.g., `dim i as integer`. Here `i` is a variable of type integer.
Object (Type of sender variable. `object` in this case, can be string, int etc)
, (Argument separator)
e (variable of second argument)
EventArgs (This represents event specific data, when the event is occured, e.g., on mouse click the position of mouse cursor in x and y co-ordinate)
Handles (Keyword)
Label1.Click (Object with its Event).
这可能会更多地纠正,但至少应该给你一个想法。
希望它有所帮助!
答案 3 :(得分:1)
Private
是方法的范围,Label1_Click
是设计器生成的方法名称,(sender As Object, e As EventArgs)
是方法参数 - 或者在这种情况下,我们称之为事件签名的事件,Handles Label1.Click
是处理此事件的委托。 Sub
是类似Function
的方法类型,它不会声明类。
参数基于它们的类型而As
关键字仅由DataType
后面的(sender As Object, e As EventArgs)
指定,发件人和e是变量名称。因此sender
是一个对象,e
是一个EventArgs
类。
答案 4 :(得分:1)
另一个为什么要看这个。
您的示例与Delegates and Events非常相关。在这种情况下,EventHandler
定义为;
Public Delegate Sub EventHandler(sender As Object, e As EventArgs)
想象一下这个类的点击事件类型为EventHandler
:
Public Class Button
Public Event Click As EventHandler
End Class
现在,如果您要订阅此活动,则需要定义为EventHandler
的目标。请注意,命名和修饰符/访问级别无关紧要,但类型不是。
' | | |
Public/Friend/Private Sub clickOrWhateverYouNameIt(senderOrWhateverYouNameIt As Object, eOrWhateverYouNameIt As EventArgs)
示例强>
Public Class Form1
Public Sub Hook()
AddHandler Me.b.Click, AddressOf Me.MyClick
'Or
AddHandler Me.b.Click, Sub(s As Object, ea As EventArgs) MsgBox("Clicked")
End Sub
Private Sub MyClick(sender As Object, e As EventArgs)
MsgBox("Clicked")
End Sub
Private b As Button
End Class
或设计师的方式:
Public Class Form1
Private Sub MyClick(sender As Object, e As EventArgs) Handles b.Click
MsgBox("Clicked")
End Sub
Private WithEvents b As Button
End Class