如何在c#windows应用程序中捕获enter键事件而不是使用图形按钮 [英] how to catch an enter key event in c# windows application rather than using a graphical button

查看:109
本文介绍了如何在c#windows应用程序中捕获enter键事件而不是使用图形按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#Windows apllication中捕获回车键事件而不是使用图形按钮?

解决方案

使用按键向下 [ ^ ]活动



  private   void  Form1_KeyDown( object  sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show( 按下输入键);
}
}


如果要在任何控件之前捕获整个表单的'Enter KeyDown事件'在表格上收到KeyDown活动:



1.将Form的KeyPreview属性设置为'true



2.为Form的KeyDown事件连接一个EventHandler:

  private   bool  IsEnterSeen =  false ; 

private void Form1_KeyDown( object sender,KeyEventArgs e)
{
IsEnterSeen = e.KeyCode == Keys.Enter;

// 如果'IsEnterSeen =='true $ b,请执行此操作
$ b}

如果您想获得整个表格的'Enter KeyPress事件,并且您想要取消'Enter键的进一步处理:

  private   char  enterChar =( char  13 ; 

private void Form1_KeyPress( object sender,KeyPressEventArgs e)
{
IsEnterSeen = e.KeyChar == enterChar;

// 如果'IsEnterSeen =='true $ b,请执行此操作
$ b // 取消对Enter键的进一步处理:
< span class =code-comment> // if(IsEnterSeen)e.Handled = true;

}


how to catch an enter key event in c# Windows apllication rather than using a graphical button?

解决方案

use key down[^] event

private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               MessageBox.Show("Enter key pressed");
           }
       }


If you want to catch the 'Enter KeyDown Event for the entire Form ... before any of the Controls on the Form receive the KeyDown Event:

1. set the Form's KeyPreview Property to 'true

2. wire-up an EventHandler for the Form's KeyDown Event:

private bool IsEnterSeen = false;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    IsEnterSeen = e.KeyCode == Keys.Enter;

    // take action here if 'IsEnterSeen == 'true

}

If you want to get the 'Enter KeyPress Event for the entire Form, and you want to cancel the further processing of the 'Enter key:

private char enterChar = (char)13;

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    IsEnterSeen = e.KeyChar == enterChar;

    // take action here if 'IsEnterSeen == 'true

    // to cancel further processing of the Enter Key:
    // if(IsEnterSeen) e.Handled = true;

}


这篇关于如何在c#windows应用程序中捕获enter键事件而不是使用图形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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