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

十年热恋 提交于 2020-01-04 01:36:11

问题


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

回答1:


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:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2133402/windows-form-tab-key-does-not-work-in-a-child-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!