同时验证 C# windows 窗体上的多个文本框 [英] Validating several textboxes on a C# windows form at the same time

查看:24
本文介绍了同时验证 C# windows 窗体上的多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个文本框和其他控件的表单.我正在使用 errorprovider 控件,并在需要验证的每个文本框上连接了验证事件.当一个控件将焦点转移到另一个控件时,就会发生 Validating 事件.因此,当您使用 Tab 键离开当前控件或使用鼠标单击另一个控件时,会发生该事件1.我觉得这非常烦人,举个例子,如果我打开这个 winform,然后立即尝试关闭它,它不会让我,因为验证会触发抱怨第一个文本框没有输入文本.

I have a form with several textboxes and other controls. I'm using the errorprovider control and I wired the validating event on each textbox that I need to validate. The Validating event occurs when a control yields focus to another control. So the event occurs when you use the tab key to move away from the current control or when you click another control with the mouse1. And I find that extremely annoying, just to give an example, if I open this winform, and then immediately try to close it, it is not going to let me, because the validation will trigger complaining that the first textbox have no text entered.

我想要的行为是使用errorprovider,如何仅在单击表单的保存"按钮时验证整个表单?

The behavior I want is using the errorprovider, how can I validate the whole form only when I click the Save button of the form?

谢谢

推荐答案

检查属性 Form.AutoValidate.

可能的值:

  • 禁用

不会发生隐式验证.设置这个值不会干扰显式调用验证或验证儿童.

Implicit validation will not occur. Setting this value will not interfere with explicit calls to Validate or ValidateChildren.

  • 启用PreventFocusChange
  • 隐式验证发生在控制失去焦点.

    Implicit validation occurs when the control loses focus.

    • 启用AllowFocusChange
    • 隐式验证发生,但如果验证失败,焦点仍将更改为新控件.如果验证失败,Validated 事件不会开火.

      Implicit validation occurs, but if validation fails, focus will still change to the new control. If validation fails, the Validated event will not fire.

      将其设置为 EnableAllowFocusChange 将解决打开表单并立即尝试关闭它的问题,因为焦点将转移到取消按钮.

      Setting it to EnableAllowFocusChange will resolve the problem of opening the form and immediatly trying to close it since the focus will be transferred to the Cancel button.

      如果您想完全控制验证,您可以将其设置为 Disable 并使用 Form.ValidateForm.ValidateChildren 执行手动验证.我不是 100% 确定,但我相信 Form.ValidateChildren 不会触发放置在 TabControl 中的控件的验证事件.

      If you want complete control over the validation you can set it to Disable and perform manual validation using Form.Validate and Form.ValidateChildren. I am not 100% sure but I believe that Form.ValidateChildren will not trigger validation events for controls placed inside a TabControl.

      这篇关于同时验证 C# windows 窗体上的多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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