Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a User Control which I've placed a FlowControlPanel on, then some other controls. Now I've probably assumed to much in expecting events that happen to the child controls, will also be sent for processing at the User Control level. Such as when someone clicks on the label that is in the control, not just the Click event on the label should fire, but the Click event on the User Control should fire as well.

I mean not only was the label clicked, but so was my user control. Maybe I'm missing something, maybe I've been entering the wrong search criteria, but is there anyway to get the events of the child controls to fire the User Controls events?
Posted
Updated 7-Feb-11 15:03pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-11 20:01pm    
Can make sense... good question, anyway, my 5.
--SA

As far as I know, there's no way to do this except via a mouse hook (which may be overkill for this). Instead you could handle the child's Click event in the UserControl class (which should basically give you the same flexibility).
 
Share this answer
 
v2
Comments
Franklin Smith 7-Feb-11 16:17pm    
The click event is just an example. Basically what I want is the control to be treated like it is an individual control and not a panel with multiple child controls.

I may just have to go into the Paint event and paint what I want on the user control. While this would solve the problem it's just a pain as I have a Label on the control that allowed for two rows of text if needed. Doing the Paint way I have to do all the coding to make it drop a line if the text extends beyond the boundaries of the control. While it's more work than I wanted to deal with. Sometimes you just do what you have to.
Nish Nishant 7-Feb-11 16:18pm    
Yes, at this point, you have 2 options :

1) Do custom painting, so you don't use actual button/label controls, instead you simulate button and label behavior yourself.

2) Use WPF where this is built-in and is an integral part of how WPF works.
Espen Harlinn 7-Feb-11 16:18pm    
a 5 - nice and simple :)
Nish Nishant 7-Feb-11 16:19pm    
Thanks Espen.
Franklin Smith 7-Feb-11 16:30pm    
I just had a thought that allows me to get away with all this. The images on the control are all the same height, width. They are more or less Icons, the issue I had with the paint was the label. I just had a thought make a label, but don't show it. Do a .DrawToBitmap() on the label then place the bitmap on control in the paint. Solves all my problems and I don't have to do the calculations for the caption if it is to long and needs to extend to the second row.

Thanks for the help guys.
Windows Forms does not support the idea of event bubbling, but you can always add an event to your UserControl, route all mouse click events for the child controls to a single event handler, and then fire the event.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 7-Feb-11 16:19pm    
Voted 5, this is pretty similar to what I myself suggested to the OP.
Espen Harlinn 7-Feb-11 16:22pm    
Well, yes - just thought that "exposing" the cliks through an event would be a nice addition.
Nish Nishant 7-Feb-11 16:27pm    
Oh yes, your answer absolutely adds to the responses, and hence the 5 :-) [sorry if that was not clear in my earlier comment]
Espen Harlinn 7-Feb-11 16:46pm    
I didn't think anything else :)
Sergey Alexandrovich Kryukov 7-Feb-11 19:54pm    
Good answer - a 5.
Such routing is a good idea when the event is important rather than detail of children, can be quite practical.
--SA
Why do you want the click event for the UserControl to fire. Instead, make all of its child controls public so the hosting form can hook their events.

You can, of course do what you want by using interopservices and forcing a mouse click event, but the click event is the only event that would make sense, and I don't think you really want that.
 
Share this answer
 
v2
Comments
Franklin Smith 7-Feb-11 16:20pm    
I want only events on the UserControl to actually fire because I want it to behave as if the child controls don't actually exist except to paint stuff for me so I don't have to do it manually. What can I say I'm a little lazy. :)
Sergey Alexandrovich Kryukov 7-Feb-11 20:00pm    
No! Franklin, you idea makes perfect sense. If you also explain your ultimate goals, we can discuss some detail.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900