如何锁定验证事件 [英] How can I lock the Validating Event

查看:69
本文介绍了如何锁定验证事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文本框,Textbox1和Textbox2和一个用户控件(UsrControlA). UsrControlA同时取决于Textbox1和Textbox2.

现在,我需要以编程方式更改两个文本框的值.但是随后我更改了第一个UsrControlA引发错误,因为Textbox2包含相对于Textbox1的无效值.

我曾经读过一篇有关锁定两个文本框,更改值然后释放锁定的命令的信息.释放锁后,将触发验证.

有人能提醒我该命令吗?

谢谢

JP

I have 2 text boxes, Textbox1 & Textbox2 and a user control (UsrControlA). UsrControlA depends on both Textbox1 & Textbox2.

Now I need to programatically change the values of both textboxes. But then I change the first UsrControlA throws an error because Textbox2 contains invalid values with respect to Textbox1.

I once read about a command to lock both Textboxes, change the values and then release the lock. When the lock is released, the Validating is fired.

Can anyone remind me the command?

Thanks

JP

推荐答案

您是否注意到:您正在尝试解决该问题,通常情况下这是不正确的.
是的,在一般情况下,原则上这是不可能的.

您正在尝试保持两个逻辑绑定的输入值的内容的原子有效性.一般来说,它是无法解决的.您应该重新考虑设计并允许两个值暂时无效,但是在某些事件上执行验证,例如,单击按钮应用"(甚至验证").该事件应分析情况并指出我们的无效点.

也许,就您而言,这并非不可能(毕竟,如果您要问的是您可能有一个有效的用例).好吧,那么强制执行原子有效性的尝试可能会使用户感到非常困惑.因此,我仍然认为您应该考虑上述建议.

您没有共享验证规则.所以,我举一个简单的例子.

考虑一个文本框,您要在其中输入任何浮点错误并希望在每次按键时对其进行验证.用户想要输入"13.2e-2".当用户键入"13.2e"或"13.3-"时,验证立即引发错误.

因此,您有一个类似的问题.并且您已经有疑问,请不要尝试这样做.验证应按照我上面的建议进行.

—SA
Did you notice: you''re trying to solve the problem, which is in general case incorrect.
Yes, in general case this is impossible in principle.

You''re trying to keep atomic validity of the content of two logically bound input values. Generally speaking, it is not solvable. You should reconsider design and allow two values to be invalid for a while, but perform validation on some event, for example, a click on a button "Apply" (or even "Validate"). The event should analyze the situation and point our the invalid point.

Maybe, in your case it is not impossible (after all, if you''re asking you probably have a valid use case in mind). Well, then the attempt to enforce atomic validity can be very confusing to the user. So, I still think you should consider my advise above.

You did not share your validation rule. So, I''ll give you a simple example.

Consider a text box where you want to enter any floating point error and want it to be validated on every press of the key. The user wants to enter "13.2e-2". When the user types "13.2e" or "13.3-" the validation immediately throws an error.

So, you have a similar problem; and as you already have some doubt, don''t try to do it. The validation should be deferred as I advised above.

—SA


拥有类级别bool的最简单方法:
The simplest way if to have a class level bool:
private bool updatingTextBoxes = false;


在文本框更改处理程序中进行设置,在UsrControlA中对其进行检查


Set it in the textbox change handler, check it in UsrControlA


这篇关于如何锁定验证事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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