事件不触发时,C#提交文本框发生更改 [英] C# Committing Textbox changes when events don't fire

查看:133
本文介绍了事件不触发时,C#提交文本框发生更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Win Forms 2.0 c#应用程序,当使用经过验证的机制并处理Enter键时,它会在文本框中提交/处理文本.当焦点在控件之间移动时,这在驱动控件之间移动时效果很好.但是,如果我在编辑框中有新数据而没有按"Enter"键,并且选择了"File/Save"之类的菜单项,则由于焦点仍在文本框控件上,因此在离开Textbox控件时不会进行验证.未更新.当我有一个非模式对话框并且在主应用程序和对话框之间切换时,会发生相同的情况.没有焦点更改,因此不会发生验证. 离开"事件也不会触发.

这种情况应该如何处理?我看到其他可以正确执行此操作的应用程序.菜单选择,表单更改和工具栏按钮的解决方案是否相同?

谢谢

解决方案

您是否尝试过Validat ing 事件,而不是Validat ed 事件?


作为验证的替代或补充,您可以处理事件System.Windows.Forms.TextBox.TextChanged.您无法使用此事件取消更改.您只能阅读此事件的文本,如果无效,则应采取相应的措施.仔细设计此类行为.

为了能够取消文本更改,您可以处理事件KeyPress.如果使用Handled = true(此事件的事件参数的属性),则可以取消更改,因此可以从输入中过滤掉不需要的字符.

—SA


您可以尝试使用TextChanged事件或KeyUp事件.如果它是一个大文本框,但如果它只占几个字符,那么一个简单的小文本框就可以正常工作.

I have a win forms 2.0 c# app that commits/processes text in a text box when it is entered by using the validated mechanism and handling the enter key. This works great when moving between controls as the focus changes which drives these events. However, if I have new data in an edit box without pressing ''enter'' and I select a menu item like ''File/Save'' no validation occurs when leaving the Textbox control as the focus is still on it so my data is not updated. The same situation occurs when I have a non-modal dialog and I switch in between the main app and the dialog. There is no focus change so the validation doesn''t occur. The ''Leaving'' event doesn''t fire either.

How should this situation be handled? I see other apps that do this correctly. Is the solution the same for both the menu selection and form change and toolbar buttons?

Thanks

解决方案

Have you tried the Validating event rather then the Validated event?


Alternative or additional to validation you can handle event System.Windows.Forms.TextBox.TextChanged. You cannot cancel the change using this event. You can only read the text on this event and act accordingly if it is invalid. Design such behavior very carefully.

To be able to cancel a text change, you can handle the event KeyPress. If you use Handled = true (a property of this event''s event arguments), you can cancel the change, so in this way you can filter out unwanted characters from input.

—SA


You can try using the TextChanged event or KeyUp event. This can be painfully slow if it is a large text box but a small simple one taking only a few characters should work fine.


这篇关于事件不触发时,C#提交文本框发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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