重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你需要的应该是反射,类似如下,我测试通过的:
站在用户的角度思考问题,与客户深入沟通,找到榆林网站设计与榆林网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广、申请域名、虚拟空间、企业邮箱。业务覆盖榆林地区。
Imports System.Reflection
Public Class Form1
Public Sub MMMToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MMMToolStripMenuItem.Click
MsgBox("menu click")
End Sub
'在按钮事件处理里面通过反射名称调用相应菜单事件
Public Sub CallEventMethod(sender As Object, e As EventArgs)
Dim b As Button = sender '获取点击的按钮
'拼接菜单事件名称
Dim MethodName As String = b.Name "ToolStripMenuItem_Click"
Dim t As Type = Me.GetType
Dim m As MethodInfo = t.GetMethod(MethodName)
'反射方法
m.Invoke(Me, New Object() {Nothing, Nothing})
End Sub
'动态生成了按钮
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim btn As New Button
btn.Location = New Point(100, 100)
btn.Name = "MMM" '关键
btn.Text = "MMM"
'注册事件
AddHandler btn.Click, AddressOf Me.CallEventMethod
Me.Controls.Add(btn)
End Sub
End Class
'这是数组的一个方法了,可是可以给数组定义扩展方法来实现这种形式。不过要给Item每个成员能动态生成相应方法,想不出其它简单点的办法,只能一个一个的添加扩展方法。
Namespace 测试
Public Class Item
Public Number() As String
Public Name As String
Public Symbol As String
Public Value As Single
Public Unit As String
Public Description As String
Public Remarks As String
End Class
Public Class XX
Public tty() As Item
Public Sub New()
tty(0).Name = "Women"
tty(0).Value = 198000
tty(1).Name = "temen"
tty.Name("temen").Value = 99999
End Sub
End Class
Public Module Module1
System.Runtime.CompilerServices.Extension() _
Public Function Name(ByVal she As Item(), ByVal key As String) As Item '定义扩展方法功能 在net.3.5前的版本像似没有。
For Each it In she
If it.Name = key Then
Return it
End If
Next
Return Nothing
End Function
End Module
End Namespace
增加事件用 AddHandler 语句
AddHandler xxxx.click Addressof xxxx()
一个 sub 可以接受多个 click 事件
似有控件遍历窗体控件集合,或在控件集合中查询该控件名字,公用控件直接名字