vb.net怎样获取同类控件名称
在一个控件上按下鼠标,另一个控件上弹起鼠标,要获取这两个控件的名称。PrivateSubCtrl_MouseDown(ByValsenderAsSystem.Object...
在一个控件上按下鼠标,另一个控件上弹起鼠标,要获取这两个控件的名称。
Private Sub Ctrl_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
CtrlName1 = sender.Name
End Sub
Private Sub Ctrl_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs)
CtrlName2=sender.Name
msgbox(CtrlName1 + "," + CtrlName2)
End Sub
这样不行
是动态添加的多个控件 展开
Private Sub Ctrl_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
CtrlName1 = sender.Name
End Sub
Private Sub Ctrl_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs)
CtrlName2=sender.Name
msgbox(CtrlName1 + "," + CtrlName2)
End Sub
这样不行
是动态添加的多个控件 展开
2个回答
展开全部
Private ctrName As String '控件名称
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
追问
动态添加的多个(100多个,多行多列排列的)控件,从任意一个按下鼠标,任意一个上弹起鼠标行吗?
追答
当然可以,MyButton是自定义控件,继承自Button,可以继承自己需要的控件类型,MonitorControls类是需要监视的控件集合,将需要监视的自定义控件添加到集合内,通过自定义事件CtrlsClick来输出鼠标左键按下和松开时MonitorControls集合内相应的控件信息,自己另外自定义需要监视的控件必须按MyButton类的函数来编写。
代码太长了放不下,看附件!
Private WithEvents _Ctrls As MonitorControls
Private Sub Form_Load(sender As System.Object, e As System.EventArgs) _
Handles MyBase.Load
Me._Ctrls = New MonitorControls
Dim but As MyButton
For i As Integer = 0 To 2 '添加控件
but = New MyButton(Me._Ctrls)
but.Name = "Button" & (i + 1).ToString
but.Text = but.Name
but.Bounds = New Rectangle((i * 56 + 6), 10, 50, 24)
Me.Controls.Add(but)
Next
End Sub
Private Sub Form_Closed(sender As System.Object, e As System.EventArgs) _
Handles MyBase.FormClosed
Me._Ctrls.Dispose()
End Sub
Private Sub CtrlsClick(sender As Object, e As CtrlClickEventArgs) _
Handles _Ctrls.CtrlClick
MsgBox(e.DownCtrl.Name & " | " & e.UpCtrl.Name) '输出值
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |