jibarz
jibarz

Reputation: 13

Raise user control click when hitting a control inside

I've created an user control with some labels and pictures inside.

Then, I added this user control to a form.

In the form events, I've set the UserControl_click event to raise a function.

The problem I'm having is, if I click to the UserControl background, where there's no label nor pictures, the function is raised. But if I click to a label or to a picture inside the control, the function doesn't raise.

I want the same behavior when clicking a control inside my user control than when clicking the control background.

Upvotes: 1

Views: 141

Answers (2)

SysDragon
SysDragon

Reputation: 9888

You have to add the add the function on each control click event inside your UserControl:

Public Event ControlClick(sender As Object, e As EventArgs)

Private Sub uc1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For each elem As Control in Me.Controls
        AddHandler elem.Click, AddressOf RaiseClick
    Next
End Sub

Private Sub RaiseClick(sender As Object, e As EventArgs)
    RaiseEvent ControlClick(sender, e)
End Sub

And then outside just catch both events:

Public Sub UserControl_click(sender As Object, e As EventArgs) _
                           Handles UserControl1.Click, UserControl1.ControlClick
    '...
End Sub

Upvotes: 0

Michael Domashchenko
Michael Domashchenko

Reputation: 1480

Your labels capture click events. Subscribe for their Click events and call the same handler that you call from UserControl_click.

Upvotes: 1

Related Questions