c# - KeyPressed 事件调用其他事件

标签 c# winforms events

我的 WinForm 上有一个确定按钮。

我想提供点击确定按钮的选项,也可以通过输入按。

这是我的代码:

void MyFormKeyPress(object sender, KeyPressEventArgs e)

    {
       if (e.KeyChar==13)

          {
            //What have I to do in order to call ButtonOK click event?
          }
    }

谢谢

最佳答案

您可以设置 Form.AcceptButton按钮的表单属性。

This property enables you to designate a default action to occur when the user presses the ENTER key in your application. The button assigned to this property must be an IButtonControl that is on the current form or located within a container on the current form.

例子:

那么你的表单名称是form1,你的按钮是button1

 form1.AcceptButton = button1;

应该做你想做的。

或者您可以使用您现有的代码并调用

   yourButton.PerformClick();

关于c# - KeyPressed 事件调用其他事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21304741/

相关文章:

c# - 如何在 WPF 中调整 Canvas 的大小?

c# - 没有在 angularjs 文件中获取 session 值

c# - Excel 无法计算状态栏中 C# 生成的文件的总和

c# - 如何启用映射实体的私有(private)属性

javascript - 从 html 事件调用 javascript 类方法的正确方法

c# - 有和没有 "new"的接线事件之间的区别

c# - 程序在 oWord.Documents.Open(oTemplatePath) 处卡住?

c# - 如何将嵌套命名空间用于自定义控件?

winforms - 如何在 C#.net winforms 中验证文本框

jquery - 创建对象时将参数传递给 jQuery 事件处理程序