0

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
Calaf
  • 1,133
  • 2
  • 9
  • 22
  • 2
    [`MyBase.OnClick()`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.onclick?view=netcore-3.1#System_Windows_Forms_Control_OnClick_System_EventArgs_)? – GSerg Jul 17 '20 at 14:26
  • @gserg Thank you so much! I changed `w.MouseClick` in `w.Click` and `Me.OnClick(e)` works perfectly! – Calaf Jul 17 '20 at 14:33

1 Answers1

1

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Yes. I had also to change `w.MouseClick` in `w.Click` – Calaf Jul 17 '20 at 14:35
  • You could also derive your own [custom Label control](https://stackoverflow.com/a/8635626/2330053) that ignores the mouse and then all the events would just pass right through to the parent (TableLayoutPanel). But then you wouldn't be able to trap mouse events for the Label itself. Could be helpful in your situation, who knows... – Idle_Mind Jul 17 '20 at 14:39