点击面板会触发包含标签的点击事件

时间:2016-08-19 01:33:10

标签: vb.net events

我制作了一个自定义日历,但我遇到了一些麻烦。正如标题所示,我想在点击其面板时触发点击事件。这是我的代码。

点击活动:

Private Sub label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m1.Click, m2.Click, m3.Click, m4.Click, m5.Click, m6.Click, tu1.Click, tu2.Click, tu3.Click, tu4.Click, tu5.Click, tu6.Click, w1.Click, w2.Click, w3.Click, w4.Click, w5.Click, w6.Click, th1.Click, th2.Click, th3.Click, th4.Click, th5.Click, th6.Click, f1.Click, f2.Click, f3.Click, f4.Click, f5.Click, f6.Click, sa1.Click, sa2.Click, sa3.Click, sa4.Click, sa5.Click, sa6.Click, su1.Click, su2.Click, su3.Click, su4.Click, su5.Click, su6.Click

    Dim clickedLabel = TryCast(sender, Label)
    If clickedLabel IsNot Nothing Then
        If clickedLabel.ForeColor = Color.Black Then Exit Sub
        clickedLabel.ForeColor = Color.Green
    End If
       End Sub

enter image description here

2 个答案:

答案 0 :(得分:0)

这是一个例子。您可能需要调整代码才能找到标签。

Private Sub panel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim panel = CType(sender, Panel)
    Dim label = panel.Controls.OfType(Of Label).Single
    label_Click(label, e)
End Sub

这是动态附加处理程序的示例。您需要再次调整代码以查找面板/标签,但它将保存您必须添加所有句柄子句。

Private Sub AddHandlers()
    For Each panel In Me.Controls.OfType(Of Panel)()
        Dim label = panel.Controls.OfType(Of Label).Single
        AddHandler panel.Click, AddressOf panel_Click
        AddHandler label.Click, AddressOf label_click
    Next

End Sub

答案 1 :(得分:0)

只需将其添加到您的代码中即可。

label1_Click(sender, e)