I.Waheed
I.Waheed

Reputation: 147

Raising event in UserControl Class

So, I have a UserControl where I have a handle for button click:

Private Sub Forward_Click(sender As Object, e As EventArgs) Handles Forward.Click

and a second button click:

Private Sub back_Click(sender As Object, e As EventArgs) Handles Back.Click

I have another handle for a mouse click on the userControl:

Private Sub UserControl_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick

Depending on the condition, I want to raise event of one of the button clicks from this sub function. All these handles are in the same UserControl Class. How would it be implementable.

Upvotes: 3

Views: 108

Answers (1)

Nathan
Nathan

Reputation: 889

You can check for which button was clicked by comparing e.Button to MouseButton.Left or MouseButton.Right. This would make sure that you only run code when the left mouse button was clicked.

    If e.Button = MouseButtons.Left Then
        'Any user code here
    End If

You can call another event handler by using its method name just like calling any other method.

Forward_Click(Me, Nothing)

To put it all together it would look something like this.

    Private Sub UserControl1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
    If e.Button = MouseButtons.Left Then
        Forward_Click(Me, Nothing)
    ElseIf e.Button = MouseButtons.Right Then
        Back_Click(Me, Nothing)
    End If
End Sub

Your conditions may be different as this is just an example. It also may not be "Best Practice" but I am not here to argue what "Best Practice" is. This is a working example to answer your question. Hope it helps.

Upvotes: 1

Related Questions