Windows Form - Tab key does not work on child panel

I have a child panel in a form that contains some text fields and buttons. I tried setting the tabstop and tabindex properties for these controls so that the user can move from one control to another. But for some reason, the tab stops working, the cursor remains in the same field that has focus when I press the tab key. I am using C # with .Net 3.5 framework. Below is what 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);
+3
source share
4 answers

Another possible problem is that the form in which "tabbing" does not work is in a form that does not display modally.

"tabbing" , .show, .ShowDialog.

+5

TabStop = true .

, , winforms , .

"" , , .

+1

( .Show()), keyDown:

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

KeyPreview True.

+1

Make sure you also set tabindex for shortcuts even though it doesn't focus.

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

  • Browse menu
  • Menu item "Order Order"

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

Hope this helps, Regards, Tom.

0
source

Source: https://habr.com/ru/post/1729917/


All Articles