Detect the Tab Key Press in TextBox

前端 未结 2 1592
天涯浪人
天涯浪人 2021-02-07 12:42

I am trying to detect the Tab key press in a TextBox. I know that the Tab key does not trigger the KeyDown, KeyUp or the

2条回答
  •  眼角桃花
    2021-02-07 13:29

    Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses.

    You can handle PreviewKeyDown event of your control to handle those key strokes and set them as input key.

    private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if(e.KeyData == Keys.Tab)
        {
            MessageBox.Show("Tab");
            e.IsInputKey = true;
        }
        if (e.KeyData == (Keys.Tab | Keys.Shift))
        {
            MessageBox.Show("Shift + Tab");
            e.IsInputKey = true;
        }
    }
    

提交回复
热议问题