How to assign a shortcut key (something like Ctrl+F) to a text box in Windows Forms?
Asked Answered
U

5

10

I am building a tool using C#. It's a Windows application. I have one text box on a form, and I want to assign focus to that text box when the user presses Ctrl + F or Ctrl + S.

How do I do this?

Uncaredfor answered 23/3, 2010 at 10:32 Comment(0)
O
7

Capture the KeyDown event and place an if statement in it to check what keys were pressed.

private void form_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S)) {
        txtSearch.Focus();
    }
}
Overcareful answered 23/9, 2016 at 18:47 Comment(0)
Y
16

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 - see How to capture shortcut keys in Visual Studio .NET.

Yetah answered 23/3, 2010 at 10:35 Comment(1)
I used to go for the alternative, but this is way simpler! +1Upali
O
7

Capture the KeyDown event and place an if statement in it to check what keys were pressed.

private void form_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S)) {
        txtSearch.Focus();
    }
}
Overcareful answered 23/9, 2016 at 18:47 Comment(0)
J
2

1st thing Make sure that the Your Windows Form property is "KeyPreview=true"

2nd Thing Open Form Event Property And double click on "KeyDown" And Write The Following code inside The Body of Event:-

private void form1_KeyDown(object sender, KeyEventArgs e)
{
     if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode ==Keys.S)) 
     {
           TextBox1.Focus();
     }
}
Jarrett answered 26/1, 2019 at 6:17 Comment(0)
H
0

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.

Hendiadys answered 23/3, 2010 at 10:36 Comment(0)
C
0

One option is to assign an access key to a control with a label. You assign a shortcut based on a label related to the textbox.

To assign an access key to a control with a label

  1. Draw the label first, and then draw the other control.

-or-

Draw the controls in any order and set the TabIndex property of the label to one less than the other control.

  1. Set the label's UseMnemonic property to true.

  2. Use an ampersand (&) in the label's Text property to assign the access key for the label. For more information, see Creating Access Keys for Windows Forms Controls.

Source : https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-create-access-keys-with-windows-forms-label-controls?view=netframeworkdesktop-4.8

In the picture below, if you press ALT+Y the focus moves to the textbox.

Example

Chibcha answered 13/4, 2021 at 5:20 Comment(1)
Ok, just added more info.Chibcha

© 2022 - 2024 — McMap. All rights reserved.