多个label响应类似的事件 vb.net
我在groupbox里面放了很多很多label,现在希望我把鼠标移到任一标签上,此标签就变成粉色的,而且把标签上的text赋值给一个变量...相当于每个label都响应类...
我在groupbox里面放了很多很多label,现在希望我把鼠标移到任一标签上,此标签就变成粉色的,而且把标签上的text赋值给一个变量...相当于每个label都响应类似的事件,肯定不想一个个label写重复的代码啦~~有什么好方法么?call label1_click这个方法是不行的,因为那样赋值的就是label1的text了。
展开
3个回答
展开全部
首先对于这种批量的控件,你最好在代码中动态生成而不是一个一个手动加。
首先定义处理函数,这个函数是所有Label触发MouseMove事件时执行的。
Private Sub LabelMouseEnter(sender As Object, e As EventArgs)
If TypeOf sender Is Label Then '判断触发的是不是Label类
Dim actLabel As Label = sender '类型转换
'对actLabel改颜色、把actLabel.Text赋值等等操作
End If
End Sub
如果你动态创建Label的话,下面这些放在Form的Load事件中
Dim labels(100) As Label
For Each l As Label In labels
l = New Label()
GroupBox1.Controls.Add(l) '显示到GroupBox里
AddHandler l.MouseEnter, AddressOf LabelMouseEnter '绑定事件
Next
如果你已经手动创建好了,把下面的放到窗体的Load事件中
For Each con As Control In GroupBox1.Children
If TypeOf con Is Label Then
Dim l As Label = con
AddHandler l.MouseEnter, Addressof LabelMouseEnter
End If
Next
首先定义处理函数,这个函数是所有Label触发MouseMove事件时执行的。
Private Sub LabelMouseEnter(sender As Object, e As EventArgs)
If TypeOf sender Is Label Then '判断触发的是不是Label类
Dim actLabel As Label = sender '类型转换
'对actLabel改颜色、把actLabel.Text赋值等等操作
End If
End Sub
如果你动态创建Label的话,下面这些放在Form的Load事件中
Dim labels(100) As Label
For Each l As Label In labels
l = New Label()
GroupBox1.Controls.Add(l) '显示到GroupBox里
AddHandler l.MouseEnter, AddressOf LabelMouseEnter '绑定事件
Next
如果你已经手动创建好了,把下面的放到窗体的Load事件中
For Each con As Control In GroupBox1.Children
If TypeOf con Is Label Then
Dim l As Label = con
AddHandler l.MouseEnter, Addressof LabelMouseEnter
End If
Next
展开全部
'仿照Handles后面的写法,加上你想引发事件的Label
Private Sub F(sender As System.Object, e As System.EventArgs) Handles Label1.MouseEnter, Label2.MouseEnter
Dim lbl As Label = sender
lbl.ForeColor = Color.Red
Dim s As String = lbl.Text '变量
MsgBox(s) '测试
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
添加lab控件组。然后写代码
Option Explicit
Dim b(100) As String
Private Sub mm(a As Integer)
Me.Label1(a).ForeColor = &HFF00FF
b(a) = Me.Label1(a).Caption
End Sub
Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
mm (Index)
Print b(Index)
End Sub
Option Explicit
Dim b(100) As String
Private Sub mm(a As Integer)
Me.Label1(a).ForeColor = &HFF00FF
b(a) = Me.Label1(a).Caption
End Sub
Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
mm (Index)
Print b(Index)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询