views:

297

answers:

2

Hi, I am builing one tool using C#. Its an windows application. I have one text box on a form and I want to assign focus to that text box when user presses Ctrl+F or Ctrl+S. I dont know how to do this. Can anyone please tell me how to do this?

+2  A: 

One way is to override the ProcessCMDKey event.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
if (keyData == (Keys.Control | Keys.S)) 
{ 
MessageBox.Show("Do Something"); 
return true; 
} 
return base.ProcessCmdKey(ref msg, keyData);
}

EDIT: Alternatively you can use the keydown event - How to capture shortcut keys in Visual Studio .NET

Aseem Gautam
A: 

Add an event that catches a key press on the form, analyse the key press and see if it matches one of your shortcut keys and then assign focus.

CeejeeB