473,581 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y 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 "ControlAdd ed" 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 15431
Hi,

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

Ken
-----------------
<br****@pacifie r.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
In a VB.Net Windows Forms application, I have a user control that
handles mouse events. Another section of code programmaticall y 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 "ControlAdd ed" 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 MapControlEvent s(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 MouseEventHandl er
Shadows Event Click As EventHandler
Shadows Event MouseUp As MouseEventHandl er
Shadows Event DoubleClick As EventHandler
Shadows Event MouseWheel As MouseEventHandl er
Shadows Event MouseMove As MouseEventHandl er
Shadows Event MouseLeave As EventHandler

Shadows Event KeyDown As KeyEventHandler
Shadows Event KeyPress As KeyPressEventHa ndler
Shadows Event KeyUp As KeyEventHandler

Shadows Event DragDrop As DragEventHandle r
Shadows Event DragEnter As DragEventHandle r
Shadows Event DragLeave As EventHandler
Shadows Event DragOver As DragEventHandle r
Shadows Event GiveFeedback As GiveFeedbackEve ntHandler

Shadows Event Validating As CancelEventHand ler
Shadows Event Validated As EventHandler
Private Sub MapControlEvent s(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 RaiseDoubleClic k
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 RaiseGiveFeedba ck

AddHandler C.Validating, AddressOf RaiseValidating
AddHandler C.Validated, AddressOf RaiseValidated
End Sub
Private Sub UnmapControlEve nts(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 RaiseDoubleClic k
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 RaiseGiveFeedba ck

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

End Sub

Private Sub RaiseEnter(ByVa l sender As Object, ByVal e As EventArgs)
RaiseEvent Enter(Me, EventArgs.Empty )
End Sub
Private Sub RaiseLeave(ByVa l sender As Object, ByVal e As EventArgs)
RaiseEvent Leave(Me, EventArgs.Empty )
End Sub
Private Sub RaiseGotFocus(B yVal 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.PointToClien t(Me.MousePosit ion)
Dim m As New MouseEventArgs( e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseDown(Me, m)
End Sub
Private Sub RaiseClick(ByVa l sender As Object, ByVal e As EventArgs)
RaiseEvent Click(Me, EventArgs.Empty )
End Sub
Private Sub RaiseMouseUp(By Val sender As Object, ByVal e As
MouseEventArgs)
Dim p As Point = Me.PointToClien t(Me.MousePosit ion)
Dim m As New MouseEventArgs( e.Button, e.Clicks, p.X, p.Y, e.Delta)
RaiseEvent MouseUp(Me, m)
End Sub
Private Sub RaiseDoubleClic k(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.PointToClien t(Me.MousePosit ion)
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.PointToClien t(Me.MousePosit ion)
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(By Val sender As Object, ByVal e As KeyEventArgs)
RaiseEvent KeyDown(Me, e)
End Sub
Private Sub RaiseKeyPress(B yVal sender As Object, ByVal e As
KeyPressEventAr gs)
RaiseEvent KeyPress(Me, e)
End Sub

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

Private Sub RaiseDragDrop(B yVal sender As Object, ByVal e As
DragEventArgs)
Dim p As Point = Me.PointToClien t(Me.MousePosit ion)
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.PointToClien t(Me.MousePosit ion)
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(B yVal sender As Object, ByVal e As
DragEventArgs)
Dim p As Point = Me.PointToClien t(Me.MousePosit ion)
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 RaiseGiveFeedba ck(ByVal sender As Object, ByVal e As
GiveFeedbackEve ntArgs)
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
1668
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 event that does not bubble... the onLoad event. For instance:
4
3005
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 any. This isn't true in FF or Opera, nor was it true in IE 5. If there is any logic in this behavior, please edify. It seems a bug to me. nf
3
10566
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 userControl, I can detect the movement using MouseMove event. the problem is when the mouse is on the child control : I can t detect
6
30034
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. scot
8
2773
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) Debug.WriteLine(Me.Bounds.Contains(e.X, e.Y)) End Sub
5
2813
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 problem I am having is when clicking (and holding mouse button) on the form's caption/title bar the download just stops until I let go of the mouse...
5
4888
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 the picturebox control's borderstyle to Fixed3D, but upon doing so, I am not able to track the picturebox control through mousemove events (if you...
2
9139
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 control is in Enable = false state the on click event is not called but that event is passed to parent object. How to convince my custom button class not...
2
3247
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 events in my main application. Specifically, when a user double clicks on my custom control I need to draw something on another picture box...
0
7883
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8158
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7917
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6567
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.