gary
gary

Reputation: 289

Windows Form - Tab key does not work in a child panel

I have a child panel in a form which contains some text boxes and buttons. I tried setting tabstop and tabindex properties for these controls so that the user can tab from one control to the next. But for some reason the tabbing does not work, the curor stays on the same field which has the focus when I press the tab key. I am using C# with .Net 3.5 framework. Below is how my code looks like -

  rightPanel.Controls.Clear();
        marketMessageLabel = new Label();
        marketMessageLabel.Location = new Point(0, 20);            
        marketMessageLabel.AutoSize = false;
        marketMessageLabel.Size = new Size(rightPanel.Width, 42);
        marketMessageLabel.BackColor = Color.White;            
        marketMessageLabel.Font = new System.Drawing.Font("Verdana", 8.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        rightPanel.Controls.Add(marketMessageLabel);                        

        signinUserNameLabel = new Label();
        signinUserNameLabel.Location = new Point(0, 150);
        signinUserNameLabel.Size = new Size(60, 14);
        signinUserNameLabel.BackColor = Color.White;
        signinUserNameLabel.Text = "User Name";            
        signinUserNameLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        rightPanel.Controls.Add(signinUserNameLabel);

        signinUserNameTextBox = new TextBox();
        signinUserNameTextBox.Location = new Point(0, 170);
        signinUserNameTextBox.Width = this.Width - 80;
        signinUserNameTextBox.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));         
        signinUserNameTextBox.TabIndex = 0;
        signinUserNameTextBox.TabStop = true;

        rightPanel.Controls.Add(signinUserNameTextBox);

        signinPasswordLabel = new Label();
        signinPasswordLabel.Location = new Point(0, 192);
        signinPasswordLabel.Size = new Size(100, 14);
        signinPasswordLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        signinPasswordLabel.BackColor = Color.White;
        signinPasswordLabel.Text = "Password";            
        rightPanel.Controls.Add(signinPasswordLabel);                      

        signinPasswordTextBox = new TextBox();
        signinPasswordTextBox.Location = new Point(0, 210);
        signinPasswordTextBox.Width = this.Width - 80;            
        signinPasswordTextBox.PasswordChar = '*';
        signinPasswordTextBox.TabIndex = 1;
        signinPasswordTextBox.TabStop = true;
        rightPanel.Controls.Add(signinPasswordTextBox);

        signInButton = new Button();
        signInButton.Text = "Sign In";
        signInButton.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        signInButton.Width = 70;            
        signInButton.BackColor = Color.White;
        signInButton.Location = new Point(0,240);
        signInButton.Click += new EventHandler(signInButton_Click);
        signInButton.TabIndex = 2;
        signInButton.TabStop = true;
        rightPanel.Controls.Add(signInButton);

Upvotes: 3

Views: 6650

Answers (4)

Pierre Carbonnelle
Pierre Carbonnelle

Reputation: 2615

If the form is modeless (displayed with .Show()), then you need to add the following code to handle the keyDown event:

    private void YourForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab)
        {
            if (e.Modifiers == Keys.Shift)
                this.ProcessTabKey(false);
            else
                this.ProcessTabKey(true);
            e.SuppressKeyPress= true;
        }
    }

You also need to set the KeyPreview property to True.

Upvotes: 3

Louis de Klerk
Louis de Klerk

Reputation: 61

Another possible problem is if the form where the "tabbing" does not work is on a form that is not displayed modally.

For some reasons, "tabbing" sometimes does not work if a child form is displayed with .show, and you'd rather display the form with .ShowDialog.

Upvotes: 6

Lenny Sirivong
Lenny Sirivong

Reputation: 1031

The solution is setting TabStop = true on the panel.

I just ran a little test, and it seems that winforms won't tab into the child panel if there are no other focusable controls outside of the panel.

You won't actually end up tabbing "onto" the panel, but it gets you around this issue you're seeing and it will tab to it's first child control.

Upvotes: 1

t0mm13b
t0mm13b

Reputation: 34592

Make sure you set the tabindex for the labels also, despite it is not focusable.

From VS designer window, with your form on the screen in design more, click on

  • View Menu
  • Tab Order menu option

point and click to set the sequential order of the controls (including labels).

Hope this helps, Best regards, Tom.

Upvotes: 0

Related Questions