There's nothing out of the box to do this, AFAIK. You'd need to do it yourself. However, it's not that difficult, you just need to check for a hotkey by overriding the ProcessCmdKey
method and then call Control.Focus()
for the appropriate control:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.D1))
{
textBox1.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
You can even take it a step further to have shortcuts for several controls and also have the ability to manage the controls and their shortcuts at run-time by having a dictionary that holds the shortcuts and the controls to be focused:
Dictionary<Keys, Control> FocusShortcuts;
public Form1()
{
InitializeComponent();
FocusShortcuts = new Dictionary<Keys, Control>();
FocusShortcuts.Add(Keys.Control | Keys.D1, textBox1);
FocusShortcuts.Add(Keys.Control | Keys.D2, textBox2);
FocusShortcuts.Add(Keys.Control | Keys.D3, textBox3);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Control control;
if (FocusShortcuts.TryGetValue(keyData, out control))
{
control.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Update
If instead, you want to set the focus to a control by its Tab Order, you can replace textBox1.Focus();
with something like this:
int someIndex = 5;
Control control = this.Controls.OfType<Control>()
.FirstOrDefault(c => c.TabIndex == someIndex);
if (control != null) control.Focus();
You'd just need to change the value of someIndex
to the index of your choice and change this
with the control's container (you can leave it if the container is the current form/UserControl).