How to create Alt shortcuts in a Windows Forms application?

I would like to create keyboard shortcuts for some controls in a Windows Forms application.

Example:

Screenshot of XYZ

Note the underlined, FEVP B.

I have a label and a text box control. I would like to associate this Alt keyboard shortcut with a shortcut and text box. Therefore, if someone presses Alt + B , focus is assigned to the associated text box. Is there any way to create this association?

+8
c # winforms shortcut
source share
3 answers

When a tag receives focus from pressing its accelerator key (set with & ), it directs focus to the next control in tab order, since the tags are not edited. You need the text box to be the next control in tab order.

To view and correct the tab order in your form, use the Order + Tab Order command in the IDE. Using TabPages or other containers adds a level of nesting to the tab order (for example, 1.1 , 1.2 instead of just 1 and 2 ), but if the label and text box are in the same container, t is too difficult to set correctly.

+11
source share

Type &File or &Edit and you will get an underscore. This will automatically associate underlined letters with the Alt keyword for quick access.

EDIT. Your question has changed, so I would like to keep up with my answer. You would like to catch the key combination ( Alt + F ) and set the focus to the text box.

You can try this solution using the KeyDown event of the main form.

  private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode == Keys.F) { this.textBox1.Focus(); } } 

To do this, you need to additionally set the KeyPreview property of the form true .

+5
source share
 this.KeyDown += new KeyEventHandler(Form1_KeyDown); void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode == Keys.W) { btnShowConstructionCdFun(); } } 
0
source share

Source: https://habr.com/ru/post/650795/


All Articles