BUG: Focus is on the hidden child control while the Visible property of the parent is set to False (814348)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

You can set the Visible property of a parent control to False at run time. If you do this, the focus is not set to any visible control. The focus is not moved to the next visible control in the tab order. For example, you have a Button control in a Panel control. At run time, you set the Visible property of the panel to False on the Click event procedure of the Button control. The focus is still on the Button control. The focus is not moved to the next visible control. This occurs when you hide the parent control.

CAUSE

When the focus is on a child control and you hide the parent control, the focus moves to the first child of the parent control. Therefore, the focus remains on the hidden control. The focus is not moved to the next visible control on the form.

WORKAROUND

To work around this bug, set the focus to the next visible control on the form. You must do this either before or after you hide the parent control. To set the focus to the next visible control on the form, follow these steps:
  1. Double-click Button1. Replace the existing code with the following code:

    Visual Basic .NET or Visual Basic 2005 Code
    ' Hide the Parent control
    Me.Button1.Parent.Visible = False
    ' Set the Focus to next control
    SelectNextControl(Button1, True, True, True, True)
    Visual C# .NET or Visual C# 2005 Code
    // Hide the Parent control
    this.button1.Parent.Visible = false;
    // Set the Focus to next control
    SelectNextControl(button1,true,true,true,true);
  2. Double-click Button2. Replace the existing code with the following code:

    Visual Basic .NET or Visual Basic 2005 Code
    ' Set the Focus to next control
    SelectNextControl(Button2, True, True, True, True)
    ' Hide the Parent control
    Me.Button2.Parent.Visible = False
    
    Visual C# .NET or Visual C# 2005 Code
    // Set the Focus to next control
    SelectNextControl(button2,true,true,true,true);
    // Hide the Parent control
    this.button2.Parent.Visible = false;
    

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Run Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Microsoft Windows application by using Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET.

    By default, Form1 is created.
  2. From the toolbox, drag a Panel control to Form1.

    Panel1 is created on Form1.
  3. From the toolbox, drag a Button control to Panel1.

    Button1 is created in Panel1.
  4. Double-click Button1, and then add the following code to the Click event procedure:

    Visual Basic .NET or Visual Basic 2005 Code
     Me.Button1.Parent.Visible = False
    Visual C# .NET or Visual C# 2005 Code
    this.button1.Parent.Visible = false;
  5. Add another Button to Form1.

    Button2 is created.
  6. On the Debug menu, click Start.
  7. Click Button1. Notice that Panel1 is hidden, but the focus is not on Button2.

REFERENCES

For more information, see the Microsoft .NET Framework SDK documentation or visit the following MSDN Web site:

Control.Visible Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsControlClassVisibleTopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbProperties kbControl kbContainer kbbug KB814348 kbAudDeveloper