Microsoft KB Archive/814348

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

Article ID: 814348

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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:   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);   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.



Steps to Reproduce the Behavior
 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. From the toolbox, drag a Panel control to Form1.

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

Button1 is created in Panel1.</li>  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; </li> Add another Button to Form1.

Button2 is created.</li> On the Debug menu, click Start.</li> Click Button1. Notice that Panel1 is hidden, but the focus is not on Button2.</li></ol>

<div class="references_section">