Http Client Protocol Issues

If you use any of these solutions, Please let me know so I can track if any of this is useful to you! Thanks! This is an area to share observations I have made working with Http Client Protocols and the associated technologies. I currently work for the Microsoft team that supports the WinInet, WinHTTP and System.Net API's and classes associated with these technologies. This is not a replacement for Microsoft Support, but an area to discuss these technologies. These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified at Microsoft - Information on Terms of Use

How to iterate over contained controls to set a common event handler (Click) in .NET

The requirement was to assign a click event handler to all Label Controls contained in a .NET User Control.  This is a simple technique but I found there was no good documentation on how to do this. 

To illustrate how to do this, I created a simple Windows Form application in C# and added a new Windows Forms User Control to the project.  I called it AutoLabelClickControl.  I placed a label right on the control called ‘Control Label’.  I added a panel with a label on it called ‘Panel Label’ and then placed a panel contained in the first panel and put a label on it called ‘Panel in a Panel Label’.

image

The idea now is that when the control ‘AutoLabelClickControl’ is loaded, code should hook up a common click event handler for all labels.  Add a _Load event handler to your Control (not the Form) using the wizard and let’s add the necessary code:

// This is the Common Event Handler for all Label Clicks
private void LabelClickEventHandler(object sender, EventArgs e)
{
    // Ensure we can typecast correctly!
    if (sender is Label)
    {
        // Do whatever you want... in this case show the Name of the control clicked
        Label aLabel = sender as Label;
        MessageBox.Show(aLabel.Name);
    }

}

// Walk the collection recursively and find Label Controls and wire them up
private void FindLabelControlsInContainerAndAssignEvent(Control.ControlCollection aCollection)
{
    foreach (Control aControl in aCollection)
    {
        // does this control contain other controls?
        if (aControl.Controls.Count > 0)
        {
            // call this function recursively!
            FindLabelControlsInContainerAndAssignEvent(aControl.Controls);
        }

        // wire up our Common Event Handler
        if (aControl is System.Windows.Forms.Label)
        {
            aControl.Click += new System.EventHandler(LabelClickEventHandler);
        }

    }
}

private void AutoLabelClickControl_Load(object sender, EventArgs e)
{
    // When loading wire up the events for all contained Labels
    FindLabelControlsInContainerAndAssignEvent(this.Controls);
}

 

And here is the test!

image

Note:  Although you may think you cannot have a label contained in a label control, you can actually do this if you edit the Designer code (not recommended) and that is why I did not have an ‘else’ statement between these:

// does this control contain other controls?
        if (aControl.Controls.Count > 0)
        {
            // call this function recursively!
            FindLabelControlsInContainerAndAssignEvent(aControl.Controls);
        }

// LOOK – NO ELSE STATEMENT… IN THEORY A LABEL COULD CONTAIN A LABEL BUT YOU WOULD HAVE TO HACK IT IN

        // wire up our Common Event Handler
        if (aControl is System.Windows.Forms.Label)
        {
            aControl.Click += new System.EventHandler(LabelClickEventHandler);
        }

It would be EXTREMELY weird for someone to do this but…  I have been around a while so that is why I wrote that code that way :-)

Note:  It is up to you to apply proper error checking and try/catch blocks to this code.  See the terms of use in my Blog header.

 

Please drop me a note if you find this useful!

Published Wednesday, October 14, 2009 12:16 PM by jpsanders
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker