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

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:
  1. In Visual Basic .NET or Visual Basic 2005, open Form1.vb (in Visual C# .NET, open Form1.cs).
  2. 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;
  3. On the Debug menu, click
    Start to run the application.

    Form1 is displayed.
  4. 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 "More Information" 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.

More Information

Steps to reproduce the behavior

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

    Note In Visual Studio 2005, click Visual Basic.
  4. Click Windows Application under
    Templates.
  5. Name the project TabStopDemo, and then click OK.
  6. By default, Form1 is created.
  7. On the View menu, click
    Code.
  8. 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 = "GroupBox1"
    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 = "GroupBox1"
    '
    ' Button2
    '
    Me.Button2.Location = New System.Drawing.Point(96, 24)
    Me.Button2.Name = "Button2"
    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 = "Button2"
    '
    ' Button1
    '
    Me.Button1.Location = New System.Drawing.Point(16, 24)
    Me.Button1.Name = "Button1"
    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 = "Button1"

    '
    ' 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 = "Panel1"
    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 = "Button4"
    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 = "Button4"
    '
    ' Button3
    '
    Me.Button3.Location = New System.Drawing.Point(8, 16)
    Me.Button3.Name = "Button3"
    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 = "Button3"

    '
    ' TabControl1
    '
    Me.TabControl1.Controls.Add(Me.TabPage1)
    Me.TabControl1.Location = New System.Drawing.Point(24, 128)
    Me.TabControl1.Name = "TabControl1"
    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 = "TabPage1"
    Me.TabPage1.Size = New System.Drawing.Size(176, 70)
    Me.TabPage1.TabIndex = 0
    Me.TabPage1.Text = "TabPage1"
    '
    ' Button6
    '
    Me.Button6.Location = New System.Drawing.Point(96, 16)
    Me.Button6.Name = "Button6"
    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 = "Button6"
    '
    ' Button5
    '
    Me.Button5.Location = New System.Drawing.Point(8, 16)
    Me.Button5.Name = "Button5"
    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 = "Button5"

    '
    ' 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 = "Form1"
    Me.Text = "Form1"

    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 = "Button1"
    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 = "Button1"
    '
    ' Button2
    '
    Me.Button2.Location = New System.Drawing.Point(96, 16)
    Me.Button2.Name = "Button2"
    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 = "Button2"
    '
    ' UserControl1
    '
    Me.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
    Me.Controls.AddRange(New Control() {Me.Button1, Me.Button2})
    Me.Name = "UserControl1"
    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:

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

    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 = "groupBox1";
    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 = "groupBox1";
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(96, 24);
    this.button2.Name = "button2";
    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 = "button2";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(16, 24);
    this.button1.Name = "button1";
    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 = "button1";

    //
    // 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 = "panel1";
    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 = "button4";
    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 = "button4";
    //
    // button3
    //
    this.button3.Location = new System.Drawing.Point(8, 16);
    this.button3.Name = "button3";
    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 = "button3";

    //
    // tabControl1
    //
    this.tabControl1.Controls.Add(this.tabPage1);
    this.tabControl1.Location = new System.Drawing.Point(24, 128);
    this.tabControl1.Name = "tabControl1";
    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 = "tabPage1";
    this.tabPage1.Size = new System.Drawing.Size(176, 70);
    this.tabPage1.TabIndex = 0;
    this.tabPage1.Text = "tabPage1";
    //
    // button6
    //
    this.button6.Location = new System.Drawing.Point(96, 16);
    this.button6.Name = "button6";
    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 = "button6";
    //
    // button5
    //
    this.button5.Location = new System.Drawing.Point(8, 16);
    this.button5.Name = "button5";
    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 = "button5";

    //
    // 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 = "Form1";
    this.Text = "Form1";

    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 = "button1";
    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 = "button1";
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(96, 16);
    this.button2.Name = "button2";
    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 = "button2";

    //
    // UserControl1
    //
    this.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button2, this.button1});
    this.Name = "UserControl1";
    this.Size = new System.Drawing.Size(176, 56);

    this.ResumeLayout(false);
    }
    }
    }
  9. On the Debug menu, click
    Start to run the application.

    Form1 is displayed.
  10. 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.
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.
Propriedades

ID do Artigo: 820628 - Última Revisão: 29 de abr de 2008 - Revisão: 1

Comentários