VB Net Clicking event for control

I have an user control like this:

user control 1

In that control have a Panel (include a Title label) and another Panel (include Gridview, a label).

My question: how to set clicking event for this control (means I can click everywhere in this control to perform an event)

That control is used in below user control: user control 2

Thanks for your helping!

Upvotes: 0

Views: 2347

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27322

You can do this by Raising a Click event when every control fires a click event, but this is long winded and if you add a new control you will have to remember to add the handler.

A better way is to add the click event handler for every control in the user control using a recursive method:

Public Class UserControl1
    'declare the event (must shadow as the usercontrol already has it's own click event)
    Public Shadows Event Click(sender As Object, e As EventArgs)

    Private Sub ClickEventHandlerForAllControls(sender As Object, e As EventArgs)
        RaiseEvent Click(sender, e)
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddClickHandlersForControl(Me)
    End Sub


    Private Sub AddClickHandlersForControl(ctl As Control)
        'add the click event handler for this control
        AddHandler ctl.Click, AddressOf ClickEventHandlerForAllControls
        'if the control has children (e.g. panel, form) then iterate through those and add the click event for each one
        If ctl.HasChildren Then
            For Each childCtl As Control In ctl.Controls
                AddClickHandlersForControl(childCtl)
            Next
        End If
    End Sub
End Class

EDIT: As you have stated that you want to be able to click anywhere including header rows, etc. The way to do this is with a global mouse hook (unmanaged code so less desireable).

Here is a working example of that:

Public Class UserControl1

    Private Structure MSLLHOOKSTRUCT
        Public pt As Point
        Public mouseData As Int32
        Public flags As Int32
        Public time As Int32
        Public extra As IntPtr
    End Structure

    Private _mouseHook As IntPtr
    Private Const WH_MOUSE_LL As Int32 = 14
    Private Const LEFT_MOUSE_DOWN = 513
    Private Const LEFT_MOUSE_UP = 514

    Private Delegate Function MouseHookDelegate(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.FunctionPtr)> Private _mouseProc As MouseHookDelegate
    Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As MouseHookDelegate, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
    Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
    Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr

    'declare the event (must shadow as the usercontrol already has it's own click event)
    Public Shadows Event Click(sender As Object, e As EventArgs)

    Private Sub UserControl1_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated
        HookMouse()
    End Sub

    Private Sub UserControl1_HandleDestroyed(sender As Object, e As EventArgs) Handles Me.HandleDestroyed
        UnHookMouse()
    End Sub

    Public Function HookMouse() As Boolean
        If _mouseHook = IntPtr.Zero Then
            _mouseProc = New MouseHookDelegate(AddressOf MouseHookProc)
            _mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
        End If
        Return _mouseHook <> IntPtr.Zero
    End Function

    Public Sub UnHookMouse()
        If _mouseHook = IntPtr.Zero Then Return
        UnhookWindowsHookEx(_mouseHook)
        _mouseHook = IntPtr.Zero
    End Sub

    Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
        Static downLocation As Point = New Point(-1, -1)

        Select Case wParam.ToInt32
            Case LEFT_MOUSE_DOWN
                downLocation = lParam.pt

            Case LEFT_MOUSE_UP
                'don't raise click event if we have dragged
                If lParam.pt = downLocation Then
                    'check the mouse location is inside the bounding rectangle of the usercontrol
                    Dim usercontrolLocation = New Point(Me.Parent.Location.X + Me.Location.X, Me.Parent.Location.Y + Me.Location.Y)
                    Dim usercontrolRect = New Rectangle(usercontrolLocation, Me.Size)
                    If usercontrolRect.Contains(lParam.pt) Then RaiseEvent Click(Me, New EventArgs())
                End If

        End Select
        Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
    End Function

End Class

Your click event will be raised by the usercontrolon your form:

Public Class Form1

    Private Sub UserControl11_Click(sender As Object, e As EventArgs) Handles UserControl11.Click
        MessageBox.Show("Usercontrol was clicked")
    End Sub

End Class

Upvotes: 2

yzwijsen
yzwijsen

Reputation: 374

You have to add a click event to each individual control for that to work, but luckily you can also do that automatically by looping through all the controls.

Just add the code below to your user control and it will automatically add/remove any click events to all the child controls:

public new event EventHandler Click {
    add {
        base.Click += value;
        foreach (Control control in Controls) {
            control.Click += value;
        }
    }
    remove {
        base.Click -= value;
        foreach (Control control in Controls) {
            control.Click -= value;
        }
    }
}

references:

Upvotes: 2

Related Questions