Microsoft KB Archive/820628

= You cannot set the focus to the child controls of a user control by using the TAB key =

Article ID: 820628

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
In Microsoft Visual Basic .NET or Microsoft Visual Basic 2005, you create a Windows form that has a user control, and then you set the TabStop property of the user control to False (false in Microsoft Visual C# .NET). When you run the application, you cannot set the focus on the child controls of the user control by pressing the TAB key. This behavior occurs even when the TabStop property is set to True (true in Visual C# .NET) for the child controls.



CAUSE
The UserControl class inherits from the ContainerControl class. The GroupBox class, Panel class, and TabControl class inherit from the Control class. Microsoft .NET Framework interprets the TabStop property for user controls in a different way then it interprets the TabStop property for GroupBox, Panel, and TabControl. Because of the way .NET Framework interprets the TabStop property, this behavior may seem inconsistent.



WORKAROUND
To work around this problem, set the TabStop property of the user control to True. To do this, follow these steps:  In Visual Basic .NET or Visual Basic 2005, open Form1.vb (in Visual C# .NET, open Form1.cs).  Locate and replace the following code.

Visual Basic .NET or Visual Basic 2005 code

Replace MyControl.TabStop = False with MyControl.TabStop = True Visual C# .NET code

Replace MyControl.TabStop = false; with MyControl.TabStop = true;  On the Debug menu, click Start to run the application.

Form1 is displayed. Press the TAB key several times to set the focus to the buttons on Form1.

You can set the focus to the two buttons that are the MyControl child controls.

Note These steps are based on the sample from the &quot;More Information&quot; section of this article. Therefore, the code and the file names in these steps may differ from your code and your file names.



STATUS
This behavior is by design.



Steps to reproduce the behavior
Use Visual Basic .NET, Visual Basic 2005, or Visual C# .NET to reproduce the behavior, as follows:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> On the New Project dialog box, click Visual Basic Projects under Project Types.

Note In Visual Studio 2005, click Visual Basic.</li> Click Windows Application under Templates.</li> Name the project TabStopDemo, and then click OK.</li> By default, Form1 is created.</li> On the View menu, click Code.</li>  Replace the existing code with the following code.

Visual Basic .NET or Visual Basic 2005 code Public Class Form1 Inherits System.Windows.Forms.Form

Private GroupBox1 As System.Windows.Forms.GroupBox Private Button1 As System.Windows.Forms.Button Private Button2 As System.Windows.Forms.Button Private Panel1 As System.Windows.Forms.Panel Private Button3 As System.Windows.Forms.Button Private Button4 As System.Windows.Forms.Button Private TabControl1 As System.Windows.Forms.TabControl Private TabPage1 As System.Windows.Forms.TabPage Private Button5 As System.Windows.Forms.Button Private Button6 As System.Windows.Forms.Button

Private components As System.ComponentModel.IContainer

Public Sub New MyBase.New

InitializeComponent

Dim MyControl As New UserControl1

MyControl.Location = New Point(225, 150) '       ' Set TabStop equal to False '       MyControl.TabStop = False

Me.Controls.Add(MyControl) End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

<System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent components = New System.ComponentModel.Container

Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.Button2 = New System.Windows.Forms.Button Me.Button1 = New System.Windows.Forms.Button Me.Panel1 = New System.Windows.Forms.Panel Me.Button4 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.TabControl1 = New System.Windows.Forms.TabControl Me.TabPage1 = New System.Windows.Forms.TabPage Me.Button6 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button

Me.GroupBox1.SuspendLayout Me.Panel1.SuspendLayout Me.TabControl1.SuspendLayout Me.TabPage1.SuspendLayout Me.SuspendLayout

'       ' GroupBox1 '       Me.GroupBox1.Controls.Add(Me.Button2) Me.GroupBox1.Controls.Add(Me.Button1) Me.GroupBox1.Location = New System.Drawing.Point(32, 32) Me.GroupBox1.Name = &quot;GroupBox1&quot; Me.GroupBox1.Size = New System.Drawing.Size(176, 72) Me.GroupBox1.TabIndex = 0 '       ' Set TabStop equal to False '       Me.GroupBox1.TabStop = False Me.GroupBox1.Text = &quot;GroupBox1&quot; '       ' Button2 '       Me.Button2.Location = New System.Drawing.Point(96, 24) Me.Button2.Name = &quot;Button2&quot; Me.Button2.Size = New System.Drawing.Size(64, 24) Me.Button2.TabIndex = 1 '       ' Set TabStop equal to True '       Me.Button2.TabStop = True Me.Button2.Text = &quot;Button2&quot; '       ' Button1 '       Me.Button1.Location = New System.Drawing.Point(16, 24) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(64, 24) Me.Button1.TabIndex = 0 '       ' Set TabStop equal to True '       Me.Button1.TabStop = True Me.Button1.Text = &quot;Button1&quot;

'        ' Panel1 '        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Panel1.Controls.Add(Me.Button4) Me.Panel1.Controls.Add(Me.Button3) Me.Panel1.Location = New System.Drawing.Point(232, 40) Me.Panel1.Name = &quot;Panel1&quot; Me.Panel1.Size = New System.Drawing.Size(152, 64) Me.Panel1.TabIndex = 1 '       ' Set TabStop equal to False '       Me.Panel1.TabStop = False '        ' Button4 '        Me.Button4.Location = New System.Drawing.Point(80, 16) Me.Button4.Name = &quot;Button4&quot; Me.Button4.Size = New System.Drawing.Size(56, 24) Me.Button4.TabIndex = 1 '       ' Set TabStop equal to True '       Me.Button4.TabStop = True Me.Button4.Text = &quot;Button4&quot; '        ' Button3 '        Me.Button3.Location = New System.Drawing.Point(8, 16) Me.Button3.Name = &quot;Button3&quot; Me.Button3.Size = New System.Drawing.Size(56, 24) Me.Button3.TabIndex = 0 '       ' Set TabStop equal to True '       Me.Button3.TabStop = True Me.Button3.Text = &quot;Button3&quot;

'        ' TabControl1 '        Me.TabControl1.Controls.Add(Me.TabPage1) Me.TabControl1.Location = New System.Drawing.Point(24, 128) Me.TabControl1.Name = &quot;TabControl1&quot; Me.TabControl1.SelectedIndex = 0 Me.TabControl1.Size = New System.Drawing.Size(184, 96) Me.TabControl1.TabIndex = 2 '       ' Set TabStop equal to False '       Me.TabControl1.TabStop = False '        ' TabPage1 '        Me.TabPage1.Controls.Add(Me.Button6) Me.TabPage1.Controls.Add(Me.Button5) Me.TabPage1.Location = New System.Drawing.Point(4, 22) Me.TabPage1.Name = &quot;TabPage1&quot; Me.TabPage1.Size = New System.Drawing.Size(176, 70) Me.TabPage1.TabIndex = 0 Me.TabPage1.Text = &quot;TabPage1&quot; '        ' Button6 '        Me.Button6.Location = New System.Drawing.Point(96, 16) Me.Button6.Name = &quot;Button6&quot; Me.Button6.Size = New System.Drawing.Size(56, 24) Me.Button6.TabIndex = 1 '       ' Set TabStop equal to True '       Me.Button6.TabStop = True Me.Button6.Text = &quot;Button6&quot; '        ' Button5 '        Me.Button5.Location = New System.Drawing.Point(8, 16) Me.Button5.Name = &quot;Button5&quot; Me.Button5.Size = New System.Drawing.Size(56, 24) Me.Button5.TabIndex = 0 '       ' Set TabStop equal to True '       Me.Button5.TabStop = True Me.Button5.Text = &quot;Button5&quot;

'        ' Form1 '        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(440, 330) Me.Controls.Add(Me.TabControl1) Me.Controls.Add(Me.Panel1) Me.Controls.Add(Me.GroupBox1) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot;

Me.GroupBox1.ResumeLayout(False) Me.Panel1.ResumeLayout(False) Me.TabControl1.ResumeLayout(False) Me.TabPage1.ResumeLayout(False) Me.ResumeLayout(False) End Sub

Public Class UserControl1 Inherits System.Windows.Forms.UserControl

Private Button1 As System.Windows.Forms.Button Private Button2 As System.Windows.Forms.Button

Private components As System.ComponentModel.IContainer

Public Sub New InitializeComponent End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If           End If            MyBase.Dispose(disposing) End Sub

Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button

Me.SuspendLayout

'            ' Button1 '            Me.Button1.Location = New System.Drawing.Point(16, 16) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(56, 24) Me.Button1.TabIndex = 0 '           ' Set TabStop equal to True '           Me.Button1.TabStop = True Me.Button1.Text = &quot;Button1&quot; '            ' Button2 '            Me.Button2.Location = New System.Drawing.Point(96, 16) Me.Button2.Name = &quot;Button2&quot; Me.Button2.Size = New System.Drawing.Size(56, 24) Me.Button2.TabIndex = 1 '           ' Set TabStop equal to True '           Me.Button2.TabStop = True Me.Button2.Text = &quot;Button2&quot; '            ' UserControl1 '            Me.BackColor = System.Drawing.Color.FromArgb(255, 255, 255) Me.Controls.AddRange(New Control {Me.Button1, Me.Button2}) Me.Name = &quot;UserControl1&quot; Me.Size = New System.Drawing.Size(176, 56)

Me.ResumeLayout(False) End Sub

End Class

End Class Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

Visual C# .NET code using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace TabStopDemo {   public class Form1 : System.Windows.Forms.Form {       private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6;

private System.ComponentModel.Container components = null;

public Form1 {           InitializeComponent;

UserControl1 MyControl = new UserControl1; MyControl.Location = new Point(225, 150); //           // Set TabStop equal to false //           MyControl.TabStop = false;

this.Controls.Add(MyControl); }

protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

private void InitializeComponent {           this.groupBox1 = new System.Windows.Forms.GroupBox; this.button2 = new System.Windows.Forms.Button; this.button1 = new System.Windows.Forms.Button; this.panel1 = new System.Windows.Forms.Panel; this.button4 = new System.Windows.Forms.Button; this.button3 = new System.Windows.Forms.Button; this.tabControl1 = new System.Windows.Forms.TabControl; this.tabPage1 = new System.Windows.Forms.TabPage; this.button6 = new System.Windows.Forms.Button; this.button5 = new System.Windows.Forms.Button;

this.groupBox1.SuspendLayout; this.panel1.SuspendLayout; this.tabControl1.SuspendLayout; this.tabPage1.SuspendLayout; this.SuspendLayout;

//            // groupBox1 //            this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Location = new System.Drawing.Point(32, 32); this.groupBox1.Name = &quot;groupBox1&quot;; this.groupBox1.Size = new System.Drawing.Size(176, 72); this.groupBox1.TabIndex = 0; //           // Set TabStop equal to false //           this.groupBox1.TabStop = false; this.groupBox1.Text = &quot;groupBox1&quot;; //            // button2 //            this.button2.Location = new System.Drawing.Point(96, 24); this.button2.Name = &quot;button2&quot;; this.button2.Size = new System.Drawing.Size(64, 24); this.button2.TabIndex = 1; //           // Set TabStop equal to true //           this.button2.TabStop = true; this.button2.Text = &quot;button2&quot;; //            // button1 //            this.button1.Location = new System.Drawing.Point(16, 24); this.button1.Name = &quot;button1&quot;; this.button1.Size = new System.Drawing.Size(64, 24); this.button1.TabIndex = 0; //           // Set TabStop equal to true //           this.button1.TabStop = true; this.button1.Text = &quot;button1&quot;;

//            // panel1 //            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.Controls.Add(this.button4); this.panel1.Controls.Add(this.button3); this.panel1.Location = new System.Drawing.Point(232, 40); this.panel1.Name = &quot;panel1&quot;; this.panel1.Size = new System.Drawing.Size(152, 64); this.panel1.TabIndex = 1; //           // Set TabStop equal to false //           this.panel1.TabStop = false; //            // button4 //            this.button4.Location = new System.Drawing.Point(80, 16); this.button4.Name = &quot;button4&quot;; this.button4.Size = new System.Drawing.Size(56, 24); this.button4.TabIndex = 1; //           // Set TabStop equal to true //           this.button4.TabStop = true; this.button4.Text = &quot;button4&quot;; //            // button3 //            this.button3.Location = new System.Drawing.Point(8, 16); this.button3.Name = &quot;button3&quot;; this.button3.Size = new System.Drawing.Size(56, 24); this.button3.TabIndex = 0; //           // Set TabStop equal to true //           this.button3.TabStop = true; this.button3.Text = &quot;button3&quot;;

//            // tabControl1 //            this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Location = new System.Drawing.Point(24, 128); this.tabControl1.Name = &quot;tabControl1&quot;; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(184, 96); this.tabControl1.TabIndex = 2; //           // Set TabStop equal to false //           this.tabControl1.TabStop = false; //            // tabPage1 //            this.tabPage1.Controls.Add(this.button6); this.tabPage1.Controls.Add(this.button5); this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = &quot;tabPage1&quot;; this.tabPage1.Size = new System.Drawing.Size(176, 70); this.tabPage1.TabIndex = 0; this.tabPage1.Text = &quot;tabPage1&quot;; //            // button6 //            this.button6.Location = new System.Drawing.Point(96, 16); this.button6.Name = &quot;button6&quot;; this.button6.Size = new System.Drawing.Size(56, 24); this.button6.TabIndex = 1; //           // Set TabStop equal to true //           this.button6.TabStop = true; this.button6.Text = &quot;button6&quot;; //            // button5 //            this.button5.Location = new System.Drawing.Point(8, 16); this.button5.Name = &quot;button5&quot;; this.button5.Size = new System.Drawing.Size(56, 24); this.button5.TabIndex = 0; //           // Set TabStop equal to true //           this.button5.TabStop = true; this.button5.Text = &quot;button5&quot;;

//            // Form1 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(440, 330); this.Controls.Add(this.tabControl1); this.Controls.Add(this.panel1); this.Controls.Add(this.groupBox1); this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;;

this.groupBox1.ResumeLayout(false); this.panel1.ResumeLayout(false); this.tabControl1.ResumeLayout(false); this.tabPage1.ResumeLayout(false); this.ResumeLayout(false); }

[STAThread] static void Main {           Application.Run(new Form1); }   }

public class UserControl1 : System.Windows.Forms.UserControl {       private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

public UserControl1 {           InitializeComponent; }

protected override void Dispose( bool disposing ) {           if( disposing ) {               if(components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

private void InitializeComponent {           this.button1 = new System.Windows.Forms.Button; this.button2 = new System.Windows.Forms.Button; this.SuspendLayout; //            // button1 //            this.button1.Location = new System.Drawing.Point(16, 16); this.button1.Name = &quot;button1&quot;; this.button1.Size = new System.Drawing.Size(56, 24); this.button1.TabIndex = 0; //           // Set TabStop equal to true //           this.button1.TabStop = true; this.button1.Text = &quot;button1&quot;; //            // button2 //            this.button2.Location = new System.Drawing.Point(96, 16); this.button2.Name = &quot;button2&quot;; this.button2.Size = new System.Drawing.Size(56, 24); this.button2.TabIndex = 1; //           // Set TabStop equal to true //           this.button2.TabStop = true; this.button2.Text = &quot;button2&quot;; //            // UserControl1 //            this.BackColor = System.Drawing.Color.FromArgb(255, 255, 255); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button2, this.button1}); this.Name = &quot;UserControl1&quot;; this.Size = new System.Drawing.Size(176, 56);

this.ResumeLayout(false); }   } } </li> On the Debug menu, click Start to run the application.

Form1 is displayed.</li> Press the TAB key several times to set the focus to the buttons on Form1.

You cannot set the focus to the two buttons that are the MyControl child controls.</li></ol>

This behavior may seem inconsistent. The TabStop property of your GroupBox control, Panel control, or TabControl control is set to False. Your control has a child control with the TabStop property set to True. When you press the TAB key, you can set the focus to the child control, even though the parent control has the TabStop set to False. However, if your parent control is a user control, you cannot set the focus to the child control when you press the TAB key.

<div class="references_section">