CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    May 2001
    Silicon Valley

    Question How does WinForms label control handle focus? (a newbie question, perhaps)


    There is a panel which contains 2 components: a label and a TreeView. The hierarchy, obviously, looks like this:

    panel1  // handles Enter event
       - label1
       - treeView1
    I need to do some UI updates when user starts interacting with any of these 3 controls. My attempted strategy was to catch the Enter event of the panel, which would fire when user clicks a label, or TreeView. This way, I can write only one (1) event handler instead of several. While it worked for the TreeView, it didn't work for the label. When I click the label, I don't get the Enter event on the panel. What’s different about the label control?

    I could handle the Click event of the label. At the same time, I wonder what the other solutions might be.

    Any suggestion, insight or reference is really appreciated!

    - Nick

  2. #2
    Join Date
    Oct 2005
    Seattle, WA U.S.A.

    Re: How does WinForms label control handle focus? (a newbie question, perhaps)

    Hello Kender.

    Similar to the suggestion offered in thread "Dynamic test for lable (in form)"

    the following suggestion seems to be working here on my machine where I have a panel containing a label and a treeview as you describe.

    In this particular case, clicking on either the label or the treeview places the appropriate control name ('label1' or 'treeView1') in the associated text box, demonstrating that both controls are serviced by the common handler, and that the handler is able to distinguish between the two controls.

            // ****************************************************
            // common label:Click / treeView:MouseUp handler
            // ****************************************************
            private void panel_Click(object sender, EventArgs e) {
                if (sender.GetType().Equals(label1.GetType()))
                    textBox1.Text = ((Label)sender).Text;
                    textBox1.Text = ((TreeView)sender).Name;
            }// end common-object handler
    the trick is that, in this particular case, the label event is 'Click' whereas the treeView event is 'MouseUp'
    Last edited by ThermoSight; January 30th, 2011 at 07:53 PM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: How does WinForms label control handle focus? (a newbie question, perhaps)

    Quote Originally Posted by kender_a View Post
    What’s different about the label control?
    It's read-only so the user doesn't interact with it.

    Can you be more specific with regard to "do some ui updates"? Can you describe what you are trying to do in more detail?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.