Problem
A UserControl raises by default the Click Event when a mouse button is pressed. Is there a way to prevent the event from raising when clicking on a specific area of the UserControl?
Code
Suppose that we have this specific UserControl. There is a red rectangle: I want to raise the click event only in the white area.
I've tried to override the OnMouseClick sub:
Public Class UserControl1
Dim noClickArea As New Rectangle(100, 100, 50, 50)
'Draw the red rectangle
Private Sub UserControl1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Using gr As Graphics = Me.CreateGraphics
gr.Clear(Color.White)
gr.FillRectangle(Brushes.Red, noClickArea)
End Using
End Sub
'This aims to prevent the mouseClick event on noClickArea
Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
If Not noClickArea.Contains(e.Location) Then
MyBase.OnMouseClick(e)
End If
End Sub
End Class
Here is a test Form that use this UserControl and show a message box on click:
Public Class Form1
Private Sub UserControl11_Click(sender As Object, e As EventArgs) Handles UserControl11.Click
MessageBox.Show("Click")
End Sub
End Class
Result
Overriding OnMouseClick don't produce the expected result:
How can I achieve the result I want?
