views:

465

answers:

1

I have a usercontrol that is meant to take up the entire form. I want my usercontrol to handle the TAB key press and do something rather than have the TAB move focus to another control on the form. I'm handling the KeyDown event in my usercontrol, but it doesn't fire when the TAB key is pressed.

+2  A: 
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData != Keys.Tab)
            {
              return base.ProcessDialogKey(keyData);
            }
            return false;
        }
BFree
That was quick, thanks.
MusiGenesis
It just so happens that I was using this code earlier today for something I was messing with, so I had it handy.
BFree
Personally I would reverse the logic you've used, so:if (keyData == Keys.Tab) return false;
ICR
The difference being......?
BFree
What I suggested maps better with my thinking. "If the key is tab, don't process it" rather than "if the key is not tab, process it". Personal preference is all.
ICR