472,371 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

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!

Nov 21 '05 #1
2 15122
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
-----------------
<br****@pacifier.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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!

Nov 21 '05 #2
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian Genisio | last post by:
Hello all, In IE, when an event occurs, as long as it returns true, and cancelBubble is not set, the event will bubble up through the elements in the DOM tree. Though, there is at least one...
4
by: nutso fasst | last post by:
If a div is positioned block or relative, events fire over the entire area of the div. If the div is positioned absolute they don't--they only fire over the div's text or image child elements, if...
3
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
6
by: scot_nery | last post by:
I got this working in all browsers but FF/NS. It's not picking up the event to find the mouse position. In a perfect world, you mouse over the link and a mouse bubble pops up. Thanks for help....
8
by: Tom | last post by:
Hi I am having problems working out if the mouse pointer is within the control bounds within the OnMouseMove method: Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) ...
5
by: Max | last post by:
Hi, I have written a small vb.net program that downloads a file through ftp and displays the progress of download on a form (kind of the one you see when you save file from the internet). The...
5
by: BrianW | last post by:
I am working on a program that has multiple picturebox controls that a user is allowed to move around which are contained within a panel control for visual placement. In my mousedown event, I set...
2
by: bartlomiej.szafraniec | last post by:
Hi! I'm trying to implement custom button. But I don't want to extend Button class. I want to extend Control class. I want to implement Enable disable functionality. But unfortunatly when...
2
by: ahmed.maryam | last post by:
Hello Everyone, I designed a custom control that is entirely covered by a picture box. I then dragged this custom control onto a windows form application (called main) and I need to handle mouse...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.