你需要的应该是反射,类似如下,我测试通过的:
创新互联专注于企业全网整合营销推广、网站重做改版、巴州网站定制设计、自适应品牌网站建设、H5页面制作、商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为巴州等各大城市提供网站开发制作服务。
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
用vs.net创建控件项目,其他的和vb类似...
新建项目-windows 窗体控件库
Option Explicit
Private WithEvents NewTextBox As TextBox
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
Set NewTextBox = Controls.Add("VB.TextBox", "cmdNew", Form1)
NewTextBox.Move 200, 200
NewTextBox.Width = Form1.Width - 450
NewTextBox.Height = Form1.Height - 1400
NewTextBox.Visible = True
End If
End Sub
Private Sub Command2_Click()
If NewTextBox Is Nothing Then
Exit Sub
Else
Controls.Remove NewTextBox
Set NewTextBox = Nothing
End If
End Sub
似有控件遍历窗体控件集合,或在控件集合中查询该控件名字,公用控件直接名字