Calaf
Calaf

Reputation: 1173

How to raise click event of a panel by clicking on its controls in Vb.net

I've a custom TableLayoutPanel:

Public Class CustomTLP
    Inherits TableLayoutPanel

    Private labelText As Label = New Label()

    Public Sub New([variousParam])

        [...]

        labelText.Text = "Hello Dolly!"
        Me.Controls.Add(labelText, 0, 0)

    End Sub
End Class

And, in another class, I create a new CustomTLP and its mouse click handler

Dim w As CustomTLP = New CustomTLP (Me, dName)
aFlowLayout.Controls.Add(w)
AddHandler w.MouseClick, AddressOf Me.ABeautifulOperation

The problem is that when i click on the CustomTLP label, the handler doesn't detect the event. The only solution that came to my mind is to set ABeautifulOperation as public and call it from a label-click handler, but I don't think is an elegant solution... Is there a way to raise the clickevent of the panel? Something like this ( in CustomTLP):

AddHandler labelText.Click, AddressOf labelClicked

[...]

Private Sub labelClicked(sender As Object, e As EventArgs)
    ' Raise Me.MouseClick
End Sub

Upvotes: 0

Views: 805

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

As suggested by GSerg, just call the base OnClick() method when your Label is clicked:

Private Sub labelClicked(sender As Object, e As EventArgs)
    Me.OnClick(e)
End Sub

Here's a VB version of the Custom Label that will ignore mouse events, thus allowing the parent control to handle them:

Public Class CustomLabel
    Inherits Label

    Protected Overrides Sub WndProc(ByRef m As Message)
        Const WM_NCHITTEST As Integer = &H84
        Const HTTRANSPARENT As Integer = (-1)

        If m.Msg = WM_NCHITTEST Then
            m.Result = New IntPtr(HTTRANSPARENT)
        Else
            MyBase.WndProc(m)
        End If
    End Sub

End Class

Upvotes: 1

Related Questions