Solved

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

Posted on 2007-03-28
7
903 Views
Last Modified: 2008-11-27
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
0
Comment
Question by:picsnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18811902
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.

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18811986
You can wire them all up in the Forms Load() event...

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is Panel Then
                AddHandler ctl.Click, AddressOf Something_Click
                For Each subCtl As Control In ctl.Controls
                    If TypeOf subCtl Is Label Then
                        AddHandler subCtl.Click, AddressOf Something_Click
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub Something_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Debug.Print(TypeName(sender) & " --> " & sender.name)
    End Sub

End Class
0
 
LVL 1

Expert Comment

by:juniorDev
ID: 18812096
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."
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 4

Expert Comment

by:ShannonE
ID: 18812542
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
0
 
LVL 1

Author Comment

by:picsnet
ID: 18816395
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

0
 
LVL 1

Expert Comment

by:juniorDev
ID: 18826544
If you create a custom control, you will be able to reuse it just like a class.
0
 
LVL 1

Author Comment

by:picsnet
ID: 18827391
i know.  i like doing web ones.  I'm just not that gungho about windows programming to start down that path.  
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question