文章类型: .NET
关键词: C#,Winform,回车键,enter,事件,写法,KeyCode,KeyValue,13
内容摘要: 回车键事件用于处理光标移动到下一个控件以及用于触发按钮的提交功能;回车键的键值(KeyValue)为"13";回车键的KeyCode为Keys.Enter

C#的WinForm中回车键事件写法

2016/1/26 21:05:57    来源:apple    阅读:

请体会下面的关于回车键的事件的处理方式:


示例代码1、

回车键事件用于处理光标移动到下一个控件以及用于触发按钮的提交功能。

private void txtUserID_KeyDown(object sender, KeyEventArgs e)
{
    //按下回车键
    if (e.KeyValue == 13)
    {
        txtPwd.Focus();
    }
}

private void txtPwd_KeyDown(object sender, KeyEventArgs e)
{
    //按下回车键
    if (e.KeyValue == 13)
    {
        this.btnSubmit_Click(btnSubmit, null); //在TextBox按Enter键就执行button1的单击事件 
        //如果你要用引发的话要用到api
    }
}

示例代码2、

通过下面的代码,说明回车键的键值为"13"

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyValue == 13)
    {
        MessageBox.Show("你摁下了回车");
    }
}

示例代码3、

通过下面的代码,说明回车键的KeyCode为"Enter"

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 
        this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown); 
        this.button1.Click += new EventHandler(btnOK_Click); 
    } 
    void button1_Click(object sender, EventArgs e) { } 
    void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
        if (e.KeyCode == Keys.Enter) 
        { 
            this.button1_Click(button1, null); //在TextBox按Enter键就执行button1的单击事件 
            //如果你要用引发的话要用到api 
        } 
    } 
}


↑ 上一篇文章:C#实现百度站长平台_链接提交_主动提交推送的POST推送功能 关键词:C#,百度,站长平台,链接提交,主动推送,POST推送 发布日期:2016/1/23 12:37:09
↓ 下一篇文章:c#的WinForm开发中控件DataGridView的嵌套绑定解决办法 关键词:c#,WinForm,DataGridView,嵌套绑定.. 发布日期:2016/1/27 1:55:20
相关文章:
C#的WinForm开发中完美解决窗体中的各个控件同比自动放缩大小 关键词:C#,Winform,窗体,各个控件,同比例,自动放缩大小,完美解决 发布日期:2016-01-22 19:11
c# WinForm 中英文智能提示 关键词:c#,WinForm,中英文,智能提示 发布日期:2016-01-21 11:58
过滤的DataGridView在不改变数据源的分析 关键词:c#,winforms,visual,studio,datagridview,filter,过滤.. 发布日期:2016-02-04 21:34
相关目录:.NET软件开发
我要评论
正在加载评论信息......