SlicedBread
SlicedBread

Reputation: 67

Clicking a panel will trigger the containing label's click event

I made a custom calendar and and I'm having a bit of trouble. As the title suggests, I want to trigger click event when I click its panel. Here's my code.

Click Event:

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

Upvotes: 1

Views: 1847

Answers (2)

crimson589
crimson589

Reputation: 1306

Simply add this to your code.

label1_Click(sender, e)

Upvotes: 0

FloatingKiwi
FloatingKiwi

Reputation: 4506

Here's an example. You may need to tweak the code to find the label.

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

Here's an example of dynamically attaching the handlers. Once again you'll need to tweak the code to find the panels / labels but it'll save you having to add all the handles clauses.

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

Upvotes: 1

Related Questions