如何为窗体上的各种控件添加快捷键. [英] How to add shortcut keys for various controls on the form.

查看:49
本文介绍了如何为窗体上的各种控件添加快捷键.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们从工具箱中添加菜单条控件时,我们在属性中具有快捷方式分配选项.我想以按钮等形式添加各种控件的快捷方式.

例如,我有一个带有两个按钮的表单:一个按钮是插入详细信息",另一个是删除详细信息".我想要的是,如果按下键盘上的插入按钮,则按下插入详细信息"按钮,或者调用其单击事件,同样,如果按下键盘上的删除按钮,则调用删除详细信息"按钮的单击事件.

如何分配单键或多个键组合的快捷键?

We have shortcut assigning options in properties when we add menustrip control from toolbox. I want to add shortcuts for various controls in forms like for buttons etc.

For example I have a form with two buttons: One button is "Insert Details" and other "Delete Details". I want if insert button on keyboard is pressed the button with "Insert Details" is pressed or its click event is called, similarly if delete button on keyboard is pressed the "Delete Details" button''s click event is called.

How can I assign such shortcuts keys of single key or combination of multiple keys?

推荐答案

您需要编写一个顶级按键处理程序,以检测这些按键组合并调用按钮的点击方法,或调用那些单击事件调用的方法.
You need to write a top level keypress handler that detects these key combinations and calls the click methods of your buttons, or calls the method those click events call.


using System;
using System.Windows.Forms;

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        KeyPreview = true;
        KeyDown += new KeyEventHandler(FormMain_KeyDown);
    }

    void FormMain_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Insert && e.Modifiers == Keys.None)
            buttonInsert.PerformClick();
    }

    private void buttonInsert_Click(object sender, EventArgs e)
    {
        //
    }
}


这篇关于如何为窗体上的各种控件添加快捷键.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆