473,700 Members | 2,539 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 15454
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
1671
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
3010
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
10628
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
30041
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
2784
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
2825
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 button. I've tried to do Application.DoEvents on several form events but that doesn't work. I would...
5
4898
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 move the mouse off of the control too quickly, the control no longer receives mouse events). If I...
2
9147
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 to pass Click events to parent classes when my class is in Disabled state?
2
3252
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 located in my main application. I'm going to attempt to draw the scenario because I think it will
0
8731
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7819
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5904
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4408
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2032
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.