本文介绍了防止WinForm AcceptButton处理返回键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的表单,该按钮连接到 AcceptButton 属性,以便在用户按下Return键时出现逻辑并关闭表单.

在该表单上,我显示一个动态创建的 TextBox ,当用户双击某个区域时该文本框出现,然后在用户按Return键时该对话框隐藏.

当用户在 TextBox 具有焦点的情况下按Return键时,如何防止表单处理按键?

我试图说是通过 KeyEventArgs.Handled TextBox.KeyDown 事件处理程序中处理了按键,但是是通过 Button.Click 我的接受"按钮的事件首先被触发...

解决方案

使用 TextBox Enter Leave 事件来设置将 AcceptButton 属性设置为 null (在 Enter 上),然后将按钮重新分配给它(在 Leave 上)./p>

I have a form with a button hooked up to the AcceptButton property so that logic occurs and the form is closed when the user presses the Return key.

On that form I display a dynamically created TextBox that appears when the user double-clicks a certain area then hides when the user presses Return.

How do I prevent the form from processing the key press when the user presses Return while the TextBox has focus?

I was attempting to say the key press was handled in the TextBox.KeyDown event handler via KeyEventArgs.Handled but the Button.Click event of my accept button is being fired first...

解决方案

Use the Enter and Leave events of the TextBox to set the AcceptButton property to null (on Enter) and re-assign the button to it (on Leave).

这篇关于防止WinForm AcceptButton处理返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:45