Create a Cross-Container Tab Index

后端 未结 2 1924
花落未央
花落未央 2021-01-26 01:28

I\'m facing a little problem I can\'t seem to solve. The problem is in an WinForm I have several containers (TabControls, Panels, ...).

The Tab-order within the controls

相关标签:
2条回答
  • 2021-01-26 01:46

    You could work with the Leave event of that Control, and manually set focus in the code behind to do this.

    private void textBox1_Leave(object sender, System.EventArgs e)
    {
        textBox2.Focus();
    }
    
    0 讨论(0)
  • 2021-01-26 02:06

    If you want to use the TAB key, it's better to not use the Leave event It will create some bad redirections if you change with your mouse for example.

    Better override ProcessCmdKey

    Here is a good solution :

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                bool baseResult = base.ProcessCmdKey(ref msg, keyData);
    
                //if key is tab and TextBox1 is focused then jump to TextBox2
                if (keyData == Keys.Tab && TextBox1.Focused)
                {
                    TextBox2.Focus();
                    return true;
                }
                else if (keyData == Keys.Tab && TexBox2.Focused)
                {
                    TextBox3.Focus();
                    return true;
                }
                    return baseResult;
            }
    

    Hope it helped.

    0 讨论(0)
提交回复
热议问题