Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WinForm
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 7-Feb-11 10:46am
Edited 7-Feb-11 16:03pm
Henry Minute223.9K
SAKryukov at 7-Feb-11 20:01pm
Can make sense... good question, anyway, my 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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).
Franklin Smith at 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.
Nishant Sivakumar at 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 at 7-Feb-11 16:18pm
a 5 - nice and simple :)
Nishant Sivakumar at 7-Feb-11 16:19pm
Thanks Espen.
Franklin Smith at 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.
Nishant Sivakumar at 7-Feb-11 16:32pm
Sounds like something worth trying. Not sure what pitfalls you'll encounter though :-)
SAKryukov at 7-Feb-11 19:58pm
All correct, my 5.
Mouse click is certainly an overkill. Espen's not on lack of bubbling is a good point. It's not hard to organize such "bubbling" in a universal way for certain types of (source) events: events are multicast, handlers can be added automatically, with recursion of not -- all children are known to User Control.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Espen Harlinn
Nishant Sivakumar at 7-Feb-11 16:19pm
Voted 5, this is pretty similar to what I myself suggested to the OP.
Espen Harlinn at 7-Feb-11 16:22pm
Well, yes - just thought that "exposing" the cliks through an event would be a nice addition.
Nishant Sivakumar at 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 at 7-Feb-11 16:46pm
I didn't think anything else :)
SAKryukov at 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.
Espen Harlinn at 8-Feb-11 14:51pm
Thank you SAKryukov!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Franklin Smith at 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. :)
SAKryukov at 7-Feb-11 20:00pm
No! Franklin, you idea makes perfect sense. If you also explain your ultimate goals, we can discuss some detail.

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

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220

Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 7 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100