Private ctrName As String '控件名称
目前创新互联已为1000多家的企业提供了网站建设、域名、网络空间、网站托管、服务器租用、企业网站设计、郧西网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
Private isClick As Boolean '鼠标点击状态
'注:如果已知点击目标控件的父控件,ctrParent变量可以不要。
Private WithEvents ctrParent As Control '父控件
Private Sub ControlAMouseDown(sender As Object, e As MouseEventArgs) _
Handles Button1.MouseDown
isClick = (e.Button = MouseButtons.Left _
Or e.Button = MouseButtons.Right) '左键或右键按下
If isClick Then
Dim ctr As Control = CType(sender, Control) '转换Object为控件类型
ctrName = ctr.Name '获取控件名称
ctrParent = ctr.Parent '获取控件的父控件
End If
End Sub
'增加这个父控件事件,是为了正确判别鼠标弹起时是否已进入指定目标
Private Sub ParentMouseMove(sender As Object, e As EventArgs) _
Handles ctrParent.MouseMove '如果已取消ctrParent变量,改为相应的父控件
If isClick Then isClick = False '点击状态关闭
End Sub
Private Sub ControlBMouseUp(sender As Object, e As EventArgs) _
Handles Button2.MouseEnter
If isClick Then '如果点击状态为打开
Dim ctr As Control = CType(sender, Control) '转换Object为控件类型
MsgBox(ctrName " | " ctr.Name) '弹出消息显示结果
End If
End Sub
1,添加一个模块,在模块里声明一个全局公共变量N;
2,在控件的调用窗体代码段里,把此控件名称给这个全局公共变量N赋值;
3,在窗体的Load()事件里,写代码: Me.Text = N。
直接For就行了
Dim ctl As Control
Dim lbl as Label
For Each ctl In Me.Controls
If ctl.GetType.ToString = "System.Windows.Forms.Label" Then
lbl = CType(ctl,Label)
'得到一个Label,可以对它进行赋值操作了
Msgbox lbl.Name
End If
Next