我有一些文本框数据绑定(bind)到 BindingSource 以自定义业务对象。数据绑定(bind)按照以下代码完成-
txtBookNo.DataBindings.Add("Text", bs, "BookNo", true, DataSourceUpdateMode.OnPropertyChanged, null, "G", GlobalVariables.CurrentCultureInfo);
现在我想验证用户输入。哪个事件最适合此目的 - Textbox.Validating 或 Textbox.Validated?而且,DatasourceUpdateMode- OnPropertyChanged 或 OnValidation 应该是什么?
谢谢。
最佳答案
Validating
在验证过程开始之前触发,这是放置实际验证代码的地方。Validated
在验证过程完成后被触发,旨在成为基于验证结果的地方。
如果使用 DataSourceUpdateMode.OnPropertyChanged
则:
Data source is updated whenever the value of the control property changes.
如果使用 DatasourceUpdateMode.OnValidation
则:
Data source is updated when the control property is validated, After validation, the value in the control property will also be re-formatted.
如果您只想将有效(这意味着它已被您的代码验证)数据发送到您的自定义业务对象,您应该使用 DataSourceUpdateMode.OnValidation
。
Here是一篇解释如何在 Windows 窗体中进行验证的文章。
关于c# - 验证或验证事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18544798/