Link to home
Start Free TrialLog in
Avatar of picsnet
picsnet

asked on

.Net Windows Form... Event Bubbling is what I guess it would be called... Please Read!!!

Hi, I got a windows form that has 14 panels... Each panel has 3 labels on it.  What I want to capture is when someone clicks on the label.  Right now I have to do a click event for all 14 panels and for all 42 labels.  

Is there a way, hopefully easy, that I can tell the panel to handle all the events and for the labels to just buzz off...

I really want to get away from having to have that many click events.  If you ever have to cut and paste it somewhere else and you lose all the event handlers, then it really sucks unless you copied your code into notepad first.  

Tony
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

A crude way would be to initially have a reference to all your labels stored in a List collection. Then iterate using your preferred looping method and add the handler to the current label. So...

(excuse the messy pseudocode)
labellist

foreach(label l in labellist)
     add event handler.

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of juniorDev
juniorDev

You can create your own custom control that includes the group box and the labels.  In the custom control, create the click events for each of the labels and the groupbox.  Include whatever code you want to execute.

Creating a control is just like creating a class.  It can have properties and methods.  If you want the text, size, and position of the labels to be customizable, you'll need to create the appropriate properties and methods in the control's class.  Once you've done this, you should be able to use this same control on one form as many times as you want.  Problem is solved because you actually only made one set of "click-events."
So what you're saying is you want 14 events rather than 42?  It's really easy...just click on each label on each of your forms, view the properties, click the events button on the property sheet (the lightening bolt), and on the 'Click' event type the method name that you want to be called (eg Panel1Click, Panel2Click...Panel14Click).  

If it doesn't matter which Panel the label is on, then you can type the same event handler method for each label and essentially have just one method handler
Avatar of picsnet

ASKER

It seems like all these would work.  It was between idle_minds and shannone's.   I chose idleminds because it allowed me to do something very generic that I won't ever have to do again since the panels change for each client.  

Shannon's... I'd have to redo the properties for each application.  I was hoping I was missing something and it had something like web event bubbling built in, but no such luck I guess.  

Tony

If you create a custom control, you will be able to reuse it just like a class.
Avatar of picsnet

ASKER

i know.  i like doing web ones.  I'm just not that gungho about windows programming to start down that path.