.NET TextBox - 处理 Enter 键 [英] .NET TextBox - Handling the Enter Key

查看:13
本文介绍了.NET TextBox - 处理 Enter 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET TextBox 中,根据用户输入的 Enter 键 (Keys.Enter),假设拥有导致 TextBox 本身的 Enter 键被抑制的键输入(e.Handled = true)?

What is definitively the best way of performing an action based on the user's input of the Enter key (Keys.Enter) in a .NET TextBox, assuming ownership of the key input that leads to suppression of the Enter key to the TextBox itself (e.Handled = true)?

出于此问题的目的,假设所需的行为不是按下表单的默认按钮,而是应该发生的其他一些自定义处理.

Assume for the purposes of this question that the desired behavior is not to depress the default button of the form, but rather some other custom processing that should occur.

推荐答案

添加按键事件并捕获回车键

Add a keypress event and trap the enter key

以编程方式看起来有点像这样:

Programmatically it looks kinda like this:

//add the handler to the textbox
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnterKeyPress);

然后在代码中添加处理程序...

Then Add a handler in code...

private void CheckEnterKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
        if (e.KeyChar == (char)Keys.Return)

        {
           // Then Do your Thang
        }
}

这篇关于.NET TextBox - 处理 Enter 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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