c# - 验证或验证事件?

标签 c# winforms

我有一些文本框数据绑定(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/

相关文章:

c# - 将 System.IO.Abstractions 与 DirectoryInfo 一起使用

c# - NServiceBus 警告 "No handlers could be found for message type"

c# - 跨请求持久化变量

c# - winforms c# 中的静态属性

c# - Winforms .Net 简单数据绑定(bind)不起作用

c# - ViewModels和UI

c# - 如何使用带有泛型参数的类型作为约束?

c# - FormClosingEventArgs.CloseReason 中的错误?

c# - 如何获得完美的边框并使窗口始终保持在顶部?

c# - DataGridView 项目双击