How can you create Alt shortcuts in a Windows Forms application?

前端 未结 3 2301
天涯浪人
天涯浪人 2021-02-20 09:48

I\'d like to create keyboard shortcuts for some controls in my Windows Forms application.

Example:

\"Screens

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 10:03

    Type &File or &Edit and you will get underline. That will automatically bind underlined letters with Alt keyword for shortcut.

    EDIT. You question has modified so I'd like to keep up with my answer. You would like to catch some keys combination (Alt + F) and set a focus to the text box.

    You may try this solution using 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 achieve this, you have to additionally set KeyPreview property of the form to true.

提交回复
热议问题