C# Disable the TAB key

ccharactertabs

I have this code:

this.searchInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputKeypress);

private void Keypress(object sender, KeyPressEventArgs e)
{
    // If Tab has been pressed
    if(122 == (int)e.KeyChar)
    {
        switchTab(sTab);
        MessageBox.Show(sTab);
    }
}

What it does is that it sets focus to another element.
But, when the focus is set to a TextBox, and I press TAB, it just makes a tab in the TextBox, and does not set focus on the next element.

Anyone got an idea how can I make this work?

I've tried to set e.Handled = true; but that didn't work…

Best Answer

Have you tried setting AcceptsTab on the TextBox to false?

Edit:

yep. It does not work. Strange... It still tabulates in the textbox

That makes little sense. I ran a small test app, and the tab key only brings focus away from the TextBox when its AcceptsTab and Multiline properties are both true, regardless of an event handler being defined for KeyPress.

Are you sure some other code isn't setting AcceptsTab to true? If you are, does setting Multiline to false change the tab behaviour at all? Could you post more of your relevant code?