Click here to Skip to main content
Sign Up to vote bad
good
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 - 9:46
Edited 7 Feb '11 - 15:03

Comments
SAKryukov - 7 Feb '11 - 20:01
Can make sense... good question, anyway, my 5. --SA

3 solutions

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).
  Permalink  
Comments
Franklin Smith - 7 Feb '11 - 16:17
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 - 7 Feb '11 - 16:18
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:18
a 5 - nice and simple :)
Nishant Sivakumar - 7 Feb '11 - 16:19
Thanks Espen.
Franklin Smith - 7 Feb '11 - 16:30
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 - 7 Feb '11 - 16:32
Sounds like something worth trying. Not sure what pitfalls you'll encounter though :-)
SAKryukov - 7 Feb '11 - 19:58
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. --SA
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
  Permalink  
Comments
Nishant Sivakumar - 7 Feb '11 - 16:19
Voted 5, this is pretty similar to what I myself suggested to the OP.
Espen Harlinn - 7 Feb '11 - 16:22
Well, yes - just thought that "exposing" the cliks through an event would be a nice addition.
Nishant Sivakumar - 7 Feb '11 - 16:27
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:46
I didn't think anything else :)
SAKryukov - 7 Feb '11 - 19:54
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
Espen Harlinn - 8 Feb '11 - 14:51
Thank you SAKryukov!
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.
  Permalink  
Comments
Franklin Smith - 7 Feb '11 - 16:20
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 - 7 Feb '11 - 20:00
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 475
1 OriginalGriff 283
2 Dave Kreskowiak 160
3 Mayur_Panchal 148
4 Mohammed Hameed 145
0 Sergey Alexandrovich Kryukov 8,123
1 OriginalGriff 6,173
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 7 Feb 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid