Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect tab key pressing in C#?

I want to detect when tab key is pressed in a textBox and focus the next textbox in the panel.

I have tried out keyPressed method and keyDown method. But when I run the program and debug those methods are not calling when the tab key is pressed. Here is my code.

private void textBoxName_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Tab)
    {
        textBoxUsername.Focus();
    }
}

private void textBoxName_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar==(char)Keys.Tab)
    {
        textBoxUsername.Focus();
    }
}

Please correct me.Thank you.

like image 735
Dinu Avatar asked Nov 30 '22 18:11

Dinu


2 Answers

Why do you need that complication at all? WinForms does it for you automatically. You just need to set the correct tab order.

like image 193
Fyodor Soikin Avatar answered Dec 05 '22 04:12

Fyodor Soikin


go to the properties of text box and assign correct order of tabindex

like image 34
sravan Avatar answered Dec 05 '22 05:12

sravan