How does WinForms label control handle focus? (a newbie question, perhaps)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

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

  1. #1
    Join Date
    May 2001
    Location
    Silicon Valley
    Posts
    107

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

    Folks,

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

    Code:
    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
    Location
    Seattle, WA U.S.A.
    Posts
    353

    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.

    Code:
            // ****************************************************
            // 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;
                else
                    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
    Posts
    11,358

    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

This is a CodeGuru survey question.


Featured


HTML5 Development Center