vb.net AddressOf 带参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9143385/
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
AddressOf with parameter
提问by natli
One way or another I need to link groupID (and one other integer) to the button I am dynamically adding.. any ideas?
我需要以一种或另一种方式将 groupID(和另一个整数)链接到我动态添加的按钮。有什么想法吗?
What I can do;
我可以做什么;
AddHandler mybutton.Click, AddressOf PrintMessage
Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Dynamic event happened!")
End Sub
What I can't do, but want to;
我不能做但想做的事;
AddHandler mybutton.Click, AddressOf PrintMessage(groupID)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
回答by JaredPar
There is no way to do this with AddressOf
itself. What you're looking for is a lambda expression.
没有办法用AddressOf
它自己做到这一点。您正在寻找的是一个 lambda 表达式。
AddHandler myButton.Click, Function(sender, e) PrintMessage(groupId)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
回答by Olivier Jacot-Descombes
You can create your own button class and add anything you want to it
您可以创建自己的按钮类并向其添加任何您想要的内容
Public Class MyButton
Inherits Button
Private _groupID As Integer
Public Property GroupID() As Integer
Get
Return _groupID
End Get
Set(ByVal value As Integer)
_groupID = value
End Set
End Property
Private _anotherInteger As Integer
Public Property AnotherInteger() As Integer
Get
Return _anotherInteger
End Get
Set(ByVal value As Integer)
_anotherInteger = value
End Set
End Property
End Class
Since VB 2010 you can simply write
从 VB 2010 开始,您可以简单地编写
Public Class MyButton
Inherits Button
Public Property GroupID As Integer
Public Property AnotherInteger As Integer
End Class
You can access the button by casting the sender
您可以通过投射 sender
Private Sub PrintMessage(ByVal sender As Object, ByVal e As EventArgs)
Dim btn = DirectCast(sender, MyButton)
MessageBox.Show( _
String.Format("GroupID = {0}, AnotherInteger = {1}", _
btn.GroupID, btn.AnotherInteger))
End Sub
These new properties can even be set in the properties window (under Misc
).
这些新属性甚至可以在属性窗口(在 下Misc
)中设置。
The controls defined in the current project automatically appear in the toolbox.
当前项目中定义的控件会自动出现在工具箱中。
回答by haiyyu
Use the Tag property of the button.
使用按钮的 Tag 属性。
Button1.Tag = someObject
AddressOf gets the address of a method, and thus you cannot pass parameters to it.
AddressOf 获取方法的地址,因此您不能向它传递参数。
回答by Triet Nguyen
You can use delegate which very clear for your code follow as:
您可以使用对您的代码非常清楚的委托,如下所示:
Define a delegate
定义一个委托
Public Delegate Sub ControlClickDelegate(ByVal sender As Object, ByVal e As EventArgs)
Custom button class
自定义按钮类
Public Class CustomButton
Inherits System.Windows.Forms.Button
#Region "property delegate"
Private controlClickDelegate As ControlClickDelegate
Public Property ClickHandlerDelegate As ControlClickDelegate
Get
Return controlClickDelegate
End Get
Set(ByVal Value As ControlClickDelegate)
controlClickDelegate = Value
End Set
End Property
#End Region
Public Sub RegisterEventHandler()
AddHandler Me.Click, AddressOf OnClicking
End Sub
Private Sub OnClicking(ByVal sender As Object, e As System.EventArgs)
If (Me.controlClickDelegate IsNot Nothing) Then
Me.controlClickDelegate(sender, e)
End If
End Sub
End Class
MainForm
主窗体
Public Class MainForm
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.CusButton1.ClickHandlerDelegate = AddressOf Me.btnClick
Me.CusButton1.RegisterEventHandler()
End Sub
Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
Me.TextBox1.Text = "Hello world"
End Sub
End Class
回答by Dinesh Halpage
The below worked for me:
以下对我有用:
Dim bStart = New Button With {.Text = "START"}
AddHandler bStart.Click, Function(sender, e) TriggerProcess(any Long value)
Private Function TriggerProcess(ByVal paramName As Long) As Boolean
' any processing logic
Return True
End Function
回答by Josip Sibenik
My solution:
我的解决方案:
AddHandler menuItemYear.Items(i).MouseUp, Sub() menu_year(2019)
Private Sub menu_year(ByVal intYear As Integer)
'do something
End Sub
回答by Mehrdad Ordoukhani
There are few ways to do that depending of the complexity and number of parameters required. 1. Use Tag for adding a complex structure 2. Inherit the the Button class and add the values as class members then populate them before using it. That gives you a lot more flexibility.
根据所需参数的复杂性和数量,有几种方法可以做到这一点。1. 使用 Tag 添加复杂结构 2. 继承 Button 类并将值添加为类成员,然后在使用之前填充它们。这为您提供了更大的灵活性。
If you are using web version 3. You cannot add it to Tag, but for simple values assign it to index use .Attributes.Add("name"). This gets added to the HTML tags and not the Server side. You can then use the index to access a server side structure for complex systems. 4. Use sessions to store values and store the session reference to Name attribute as described above (#3).
如果您使用的是 web 版本 3。您不能将其添加到 Tag,但对于简单的值,将其分配给索引使用 .Attributes.Add("name")。这被添加到 HTML 标签而不是服务器端。然后,您可以使用索引访问复杂系统的服务器端结构。4. 使用会话来存储值并存储对 Name 属性的会话引用,如上所述 (#3)。