c# - 将键与 Windows 窗体上的按钮相关联

标签 c# winforms

我必须在 C# 上编写一个方法,将某个键(来自键盘)与特定按钮相关联。例如,如果我按下 A,我在表单应用程序上创建的按钮应该看起来像是被按下了。

最佳答案

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.KeyPreview = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.MessageBox.Show("Ctrl-F was Pressed.");
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.F))
        {
            button1.PerformClick();
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

注意:要模拟点击动画,将Click事件做成这样:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.FlatStyle = FlatStyle.Flat;
        System.Windows.Forms.MessageBox.Show("foo");
        button1.FlatStyle = FlatStyle.Standard;
    }

它并不完美,但它确实有效。

关于c# - 将键与 Windows 窗体上的按钮相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2626530/

相关文章:

c# - 淡化面板 - Windows 窗体

c# - 当我扩展应用程序时,如何防止我的 winforms 控件重叠?

c# - 如何检查 DataGridView 中的所有行是否不为空?

c# - 如何把HH :mm:ss formatted times in CSV so Excel formats them as regular text?

c# - 如何使用 C# 在 WinForms MS Chart 中的范围条的两侧打印标签

c# - 无法转换类型 System.Collection.Generic.List<T>

c# - 征求意见 : fast hashed base class for dictonary keys

c# - 文本框验证是否允许一个“。 ”值C#

c# - 预加载一些导航属性

c# - 级联集空 Entity Framework