检查“输入”在文本框中没有触发AcceptButton行为(我之前不小心删除了这个问题) [英] Check for "Enter" in textbox without triggering AcceptButton behavior (I accidentally deleted this question before)

查看:79
本文介绍了检查“输入”在文本框中没有触发AcceptButton行为(我之前不小心删除了这个问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,抱歉在第一时间删除这个问题,我试图修改它时偶然按错了按钮...... Simon_Whale和Killzone DeathMan请接受我的applogy。

我是想知道是否可以在文本框中使用KeyDown而不触发分配给表单的AcceptButton?



在表单上有一个存储一些文本的文本框(那是令人惊讶的。

如果用户在文本框中按Enter,我需要执行一些验证。

验证失败时,不允许关闭表单。 />


所以我将KeyDown添加到文本框中,我在其中检查Enter键。



到目前为止很好。



不幸的是,如果有人在文本框中按Enter,则在我处理KeyDown之前会触发AcceptButton ...



有没有办法在不删除AcceptButton分配的情况下执行此操作?



我可以在文本框的Focus事件上更改AcceptButton的分配......但不知何故感觉不对



[更新:目前(我真的希望它只是一种解决方法)我在文本框获得焦点时将表单的AcceptButton设置为null,并在文本框再次失去焦点时将其设置回相关按钮。

虽然这实际上有效但我觉得很脏...不知何故:)]



任何输入都非常感谢,

最好的问候

Andy

Good morning all and sorry for deleting the question in the first place, I acidentally hit the wrong button when trying to modify it... Simon_Whale and Killzone DeathMan please accept my applogy.
I was wondering if it is possible to use the KeyDown in a textbox without triggering the AcceptButton assigned to the form?

On the form there is a textbox that stores some text (thats surprising).
If the user presses "Enter" within the textbox I need to perform some validation.
When validation fails it is not allowed to close the form.

So I added the KeyDown to the textbox where I check for the "Enter" key.

So far so good.

Unfortunately if somebody presses "Enter" in the textbox the AcceptButton is triggered before I can handle the KeyDown...

Is there a way to do this without removing the AcceptButton assignement?

I could change the assignement of the AcceptButton upon the Focus events of the textbox... but somehow that doesn't feel right

[UPDATE: Currently (I really hoped it would be a workaround only) I set the AcceptButton of the form to null when the textbox receives focus and set it back to the related button when the textbox looses focus again.
While this actually works I do feel dirty... somehow :)]

Any input is highly appreciated,
best regards
Andy

推荐答案

好吧,这里只是spitballing,但问题是当特定字段无效时你需要压制AcceptButton。在我看来,这里最简单的选择是禁用已设置的按钮,并仅在验证通过时启用它。一个改进是在特定TextBox具有焦点时完全删除AcceptButton,然后在验证通过或TextBox失去焦点时重新启用它。
Well, just spitballing here, but the issue is that you need to suppress the AcceptButton when a particular field is invalid. Seems to me that the easiest option here is to disable the button that's set and only enable it when validation has passed. A refinement would be to remove the AcceptButton altogether when the particular TextBox has focus, and then reenable it when the validation passes or when the TextBox loses focus.


这不是好消息我害怕,据SO帖子说它是如何工作的



表单AcceptButton的问题 [ ^ ]



阅读本MSDN文章 MSDN:Forms.AcceptButton [ ^ ]



覆盖接受按钮的唯一内容是接受回车键的控件为正常beh aviour,例如多行文本框或RichTextBox。



对不起伙伴我不知道读这些文章会有什么建议。
Its not great news I'm afraid, according to this SO post thats how it works

Trouble with form AcceptButton[^]

After reading this MSDN Article MSDN : Forms.AcceptButton[^]

the only thing that overrides the accept button is a control that accepts the enter key as "normal" behaviour such as multiline text boxs or RichTextBox.

Sorry mate I don't know what to suggest from reading these articles.


使用TextBox的Validating事件。



它会在离开控件之前检查您的验证条件是否得到满足。例如

Use the Validating event of TextBox.

It checks if your validation condition is met before leaving the control. For example
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if (textBox1.Text == "hello")
        e.Cancel = true;
}





如果用户在textBox1中键入'hello',则不会引发AcceptButton click事件,这样,你不必改变任何东西。只需将KeyDown事件中的验证更改为Validating事件。



If the user types 'hello' in textBox1, the AcceptButton click event is not raised and this way, you don't have to change anything. Just change the validation in the KeyDown event to the Validating event.


这篇关于检查“输入”在文本框中没有触发AcceptButton行为(我之前不小心删除了这个问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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