How to have .NET control ignore mouse events (or forward, or bubble, or transfer, ...)

B

bretth

In a VB.Net Windows Forms application, I have a user control that
handles mouse events. Another section of code programmatically adds a
label to the control. I would like label to ignore all events allowing
the user control to react to the mouse click.

Setting the Enabled property on the label to False comes close, but I
don't want the font color to change. Does anyone have an idea how .NET
implements the code behind the Enabled property so that I could disable
events only?

I considered using the "ControlAdded" event in the user control, but
can't see how to override the large variety of events (heck, I couldn't
even get the syntax right for the single MouseUp event). What would
the code look like to re-raise the event so that the user control
handled it?

Thanks for your thoughts!
 
K

Ken Tucker [MVP]

Hi,

I would take a look at the capture method of a control. That will
make the control recieve all mouse events (me.capture=true). When the user
leaves the control release the capture by setting the me.capture=false.
Otherwise you will recieve the forms mouse events.

Ken
 
G

Guest

brett:

Having done exactly this, our solution was to trap all of the user-type
events for the label and re-raise the events manually. It was a bit tedious
writing the code, but it works like a charm. I have pasted the code below
for you to use if you would like. Just call MapControlEvents(myLabel) in
your constructor of your user control.

I do want to ask you however, what you are designing? We have already
developed a control, the Uficon, which keeps a label attached to the input
control already. What is exciting about the Uficon is that you can change
the type of control it uses as the input control both at design time and run
time. If other people out there are trying to do the same thing, then maybe
we should be marketing this product.

Anyway, here you go. Good luck!

#Region "Handlers"
Shadows Event Enter As EventHandler
Shadows Event Leave As EventHandler
Shadows Event GotFocus As EventHandler
Shadows Event LostFocus As EventHandler

Shadows Event MouseEnter As EventHandler
Shadows Event MouseHover As EventHandler
Shadows Event MouseDown As MouseEventHandler
Shadows Event Click As EventHandler
Shadows Event MouseUp As MouseEventHandler
Shadows Event DoubleClick As EventHandler
Shadows Event MouseWheel As MouseEventHandler
Shadows Event MouseMove As MouseEventHandler
Shadows Event MouseLeave As EventHandler

Shadows Event KeyDown As KeyEventHandler
Shadows Event KeyPress As KeyPressEventHandler
Shadows Event KeyUp As KeyEventHandler

Shadows Event DragDrop As DragEventHandler
Shadows Event DragEnter As DragEventHandler
Shadows Event DragLeave As EventHandler
Shadows Event DragOver As DragEventHandler
Shadows Event GiveFeedback As GiveFeedbackEventHandler

Shadows Event Validating As CancelEventHandler
Shadows Event Validated As EventHandler


Private Sub MapControlEvents(ByVal C As Control)
AddHandler C.Enter, AddressOf RaiseEnter
AddHandler C.Leave, AddressOf RaiseLeave
AddHandler C.GotFocus, AddressOf RaiseGotFocus
AddHandler C.LostFocus, AddressOf RaiseLostFocus

AddHandler C.MouseEnter, AddressOf RaiseMouseEnter
AddHandler C.MouseHover, AddressOf RaiseMouseHover
AddHandler C.MouseDown, AddressOf RaiseMouseDown
AddHandler C.Click, AddressOf RaiseClick
AddHandler C.MouseUp, AddressOf RaiseMouseUp
AddHandler C.DoubleClick, AddressOf RaiseDoubleClick
AddHandler C.MouseWheel, AddressOf RaiseMouseWheel
AddHandler C.MouseMove, AddressOf RaiseMouseMove
AddHandler C.MouseLeave, AddressOf RaiseMouseLeave

AddHandler C.KeyDown, AddressOf RaiseKeyDown
AddHandler C.KeyPress, AddressOf RaiseKeyPress
AddHandler C.KeyUp, AddressOf RaiseKeyUp

AddHandler C.DragDrop, AddressOf RaiseDragDrop
AddHandler C.DragEnter, AddressOf RaiseDragEnter
AddHandler C.DragLeave, AddressOf RaiseDragLeave
AddHandler C.DragOver, AddressOf RaiseDragOver
AddHandler C.GiveFeedback, AddressOf RaiseGiveFeedback

AddHandler C.Validating, AddressOf RaiseValidating
AddHandler C.Validated, AddressOf RaiseValidated
End Sub
Private Sub UnmapControlEvents(ByVal C As Control)

RemoveHandler C.Enter, AddressOf RaiseEnter
RemoveHandler C.Leave, AddressOf RaiseLeave
RemoveHandler C.GotFocus, AddressOf RaiseGotFocus
RemoveHandler C.LostFocus, AddressOf RaiseLostFocus

RemoveHandler C.MouseEnter, AddressOf RaiseMouseEnter
RemoveHandler C.MouseHover, AddressOf RaiseMouseHover
RemoveHandler C.MouseDown, AddressOf RaiseMouseDown
RemoveHandler C.Click, AddressOf RaiseClick
RemoveHandler C.MouseUp, AddressOf RaiseMouseUp
RemoveHandler C.DoubleClick, AddressOf RaiseDoubleClick
RemoveHandler C.MouseWheel, AddressOf RaiseMouseWheel
RemoveHandler C.MouseMove, AddressOf RaiseMouseMove
RemoveHandler C.MouseLeave, AddressOf RaiseMouseLeave

RemoveHandler C.KeyDown, AddressOf RaiseKeyDown
RemoveHandler C.KeyPress, AddressOf RaiseKeyPress
RemoveHandler C.KeyUp, AddressOf RaiseKeyUp

RemoveHandler C.DragDrop, AddressOf RaiseDragDrop
RemoveHandler C.DragEnter, AddressOf RaiseDragEnter
RemoveHandler C.DragLeave, AddressOf RaiseDragLeave
RemoveHandler C.DragOver, AddressOf RaiseDragOver
RemoveHandler C.GiveFeedback, AddressOf RaiseGiveFeedback

RemoveHandler C.Validating, AddressOf RaiseValidating
RemoveHandler C.Validated, AddressOf RaiseValidated

End Sub

Private Sub RaiseEnter(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Enter(Me, EventArgs.Empty)
End Sub
Private Sub RaiseLeave(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Leave(Me, EventArgs.Empty)
End Sub
Private Sub RaiseGotFocus(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent GotFocus(Me, EventArgs.Empty)
End Sub
Private Sub RaiseLostFocus(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent LostFocus(Me, EventArgs.Empty)
End Sub

Private Sub RaiseMouseEnter(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent MouseEnter(Me, EventArgs.Empty)
End Sub
Private Sub RaiseMouseHover(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent MouseHover(Me, EventArgs.Empty)
End Sub
Private Sub RaiseMouseDown(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim m As New MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseDown(Me, m)
End Sub
Private Sub RaiseClick(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Click(Me, EventArgs.Empty)
End Sub
Private Sub RaiseMouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim m As New MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseUp(Me, m)
End Sub
Private Sub RaiseDoubleClick(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent DoubleClick(Me, EventArgs.Empty)
End Sub
Private Sub RaiseMouseWheel(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim m As New MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseWheel(Me, m)
End Sub
Private Sub RaiseMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim m As New MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseMove(Me, m)
End Sub
Private Sub RaiseMouseLeave(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent MouseLeave(Me, EventArgs.Empty)
End Sub

Private Sub RaiseKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
RaiseEvent KeyDown(Me, e)
End Sub
Private Sub RaiseKeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
RaiseEvent KeyPress(Me, e)
End Sub

Private Sub RaiseKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
RaiseEvent KeyUp(Me, e)
End Sub

Private Sub RaiseDragDrop(ByVal sender As Object, ByVal e As
DragEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim d As New DragEventArgs(e.Data, e.KeyState, p.X, p.Y,
e.AllowedEffect, e.Effect)
RaiseEvent DragDrop(Me, d)
End Sub
Private Sub RaiseDragEnter(ByVal sender As Object, ByVal e As
DragEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim d As New DragEventArgs(e.Data, e.KeyState, p.X, p.Y,
e.AllowedEffect, e.Effect)
RaiseEvent DragEnter(Me, d)
End Sub
Private Sub RaiseDragLeave(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent DragLeave(Me, EventArgs.Empty)
End Sub
Private Sub RaiseDragOver(ByVal sender As Object, ByVal e As
DragEventArgs)
Dim p As Point = Me.PointToClient(Me.MousePosition)
Dim d As New DragEventArgs(e.Data, e.KeyState, p.X, p.Y,
e.AllowedEffect, e.Effect)
RaiseEvent DragOver(Me, d)
End Sub
Private Sub RaiseGiveFeedback(ByVal sender As Object, ByVal e As
GiveFeedbackEventArgs)
RaiseEvent GiveFeedback(Me, e)
End Sub
Private Sub RaiseValidating(ByVal sender As Object, ByVal e As
CancelEventArgs)
RaiseEvent Validating(Me, e)
End Sub
Private Sub RaiseValidated(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Validated(Me, e)
End Sub
#End Region 'Handlers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top