.NET TextBox - 处理 Enter 键

.NET TextBox - Handling the Enter Key(.NET TextBox - 处理 Enter 键)
本文介绍了.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 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)