简体   繁体   中英

Pressing Tab key does not select next control

I have four text boxes in my winform, and I've already ordered their tab index sequentially. Also, I set their TabStop properties as true . But when I press Tab key while in filling my first textbox, it does not move to next one. For that, I even added the following part for each of them in the code:

firstTextbox.KeyDown += (sender, args) => 
{
    if(args.KeyCode == Keys.Tab)
    {
        firstTextbox.SelectNextControl(this, true, true, true, true);
    }
}

But this didn't help either. Any suggestion?

Responding to tab buttons is handled by the message loop. If you're running the form as modal, or are calling

Application.Run(myForm);

then you've got a message loop. If you're only doing

Form myForm = new Form();
myForm.Visible = true;

Then you do not have a message loop and therefore tab navigation won't work. (If you are not running the form modally, then try using the ShowDialog() method to show the form, and see if tabbing works in this case.)

If this is your issue, then this MSDN article below suggests that you either

  1. Run each Form in its own thread (so that you can call Application.Run(myForm) on it). Running each form in its own thread is a huge can of worms and should not be undertaken by the faint of heart (especially if you have preexisting or poorly designed forms).
  2. Show the dialog modally .

I've had some success with a horrible hack of setting KeyPreview on the form to true and then listening for System.Windows.Forms.Keys.Tab within the Form.OnKeyDown method (and setting KeyEventArgs.SuppressKeyPress and KeyEventArgs.Handled to true if I do handle it). (Also remember to check the KeyEventArgs.Modifiers property to see if the user is doing a [Shift]+[Tab].) The downside of this is that you rob the Control that has a focus on the opportunity to respond to the keypress since Form.KeyPreview causes the Form to get a chance to handle the key before the Control gets a chance. (I'm also not sure what would happen if you implemented this logic and had the message loop going.)

As far as what you should do when you detect the Tab Key, check out the Control.SelectNextControl function. I've been calling it like...

e.SuppressKeyPress = SelectNextControl(
    ActiveControl, //Current Control
    true,  //Move Forward? (You want false for a Shift-Tab to go backward.)
    true,  //Only stop at controls where Control.TabStop == true?
    true,  //Consider controls nested within other controls?
    true); //Wrap to the beginning of the form if you reach the end?

A person who is smarter than me might be able to set up a shared message loop or something else, but this is all I've figured out so far.

Every control in windows forms application have property

this.button1.TabIndex = 3;

you can use it to select next control if you order it correctly.

如果您的文本框是多行的,您需要将 AcceptsTab 属性设置为 false。

Perhaps your event handler is not being registered. Make sure textboxes are not ReadOnly=true, TabStop=false. Try this

    public Form1()
    {
        InitializeComponent();
        this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
    }


    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab)
        {
            textBox1.SelectNextControl(sender as Control, true, true, true, true);

        }
    }

The only way I was able to fire textBox1_KeyDown with the Tab key was by overriding IsInputKey method.

class TabTextBox : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Tab)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyData == Keys.Tab)
        {
            this.SelectedText = "    ";
        }
        else
        {
            base.OnKeyDown(e);
        }
    }

and then modify InitializeComponent():

private void InitializeComponent()
{
    ...
    this.textBox1 = new TabTextBox();
    ...
}

Forms.Control.isInputKey

I ran into a similar problem with tab order completely ignored. After two hours of digging I finally found my dumb mistake: "If mybutton.Focus Then ..." when I meant "If myButton.Focused Then...". Oops. Figured I'd share in case anyone else has this experience too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM