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

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://msdn2.microsoft.com/en-us/library/system.windows.forms.control.visible(vs.71).aspx
Propriedades

ID do Artigo: 814348 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários