We help IT Professionals succeed at work.

.NET Control - child controls (e.g. labels) do not listen to my mouseclick events

jtjli
jtjli asked
on
Medium Priority
276 Views
Last Modified: 2010-04-17
I have a user control that listens to certain onmousedown and onmouseover events.
The control also holds a few labels.

When I click on the control over those few labels, the onmousedown/over event listeners are not invoked.

So here comes my question: How to have a control including all its child to listen to all mouseclick events?
Comment
Watch Question

When I click on the control over those few labels, the onmousedown/over event listeners are not invoked.

Sounds like the control you are trying to click on is actually behind your label and so the label is catching the mouse events and not the control behind it.  You might want to reposition your controls a bit or something.

Also to programatically add event handlers you would do something like this

Addhandler label1.MouseDown, Addressof MyMousdownHandler

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Like LordWabbit suggests you can use the AddHandler, or simply define multiple Handles entries for your MouseDownProcedure... e.g.

Currently you may have

Private Sub usrControl_MouseDown(ByVal sender As Object, ByVal e As system.Windows.Forms.MouseEventArgs) Handles usrControl.MouseDown        

... code to do something

End Sub

But you could easily change to

Private Sub usrControl_MouseDown(ByVal sender As Object, ByVal e As system.Windows.Forms.MouseEventArgs) Handles usrControl.MouseDown, Label1.MouseDown, Label2.MouseDown, Label3.MouseDown          

... code to do something

End Sub


HTH

Author

Commented:
>> Sounds like the control you are trying to click on is actually behind your label and so the label is catching the mouse events and not the control behind it

If my control is above my labels, then my labels will be hidden, right?  Or is there any workaround..
>>If my control is above my labels, then my labels will be hidden, right?

Your user control is acting as a container to the labels. If you made the control higher in the Z-Order then you probably won't actually see the labels.

The behaviour you are experiencing can also be replicated by having a groupbox with a couple of labels within it.

The solution I proposed above is very straight forward so I am surprised you are still looking for another workaround, or is there some more issues we need to know about?

Author

Commented:
i've accepted that there's no direct solution
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.