3
votes

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);
4
Use a UserControl instead. And don't use Clear() without calling Dispose() on the child controls, it permanently leaks the control window handles.Hans Passant

4 Answers

5
votes

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.

2
votes

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);
        }
    }

You also need to set the KeyPreview property to True.

1
votes

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.

0
votes

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.