如何自动触发窗体上控件的Validated事件

当窗体中存在多个参数输入控件时,通常需要在确认按钮被点击时验证所有参数的有效性。通过控件的Validated事件可以实现验证,但默认需通过焦点变化触发。本文介绍了一种简便方法——使用Form的ValidateChildren方法,可以批量触发支持验证的子控件的数据验证。通过调整ValidateChildren的参数,还能控制只验证可见的控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

窗体上有很多控件,用来设置一些参数,需要在点击OK时对所有的参数进行验证,如果有参数无效则显示错误信息,效果如下:

 

 

 

首先,需要为控件添加验证事件

 

 

正常情况下,应该是控件的焦点发生变化时才会触发Validated事件,下面是MSDN的一些资料

 


 

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

If the Cancel property of the CancelEventArgs is set to true in the Validating event delegate, all events that would usually occur after the Validating event are suppressed.

For more information about handling events, see Consuming Events.

 


 

 

 

但是由于控件较多,总不能一个一个去改变焦点,而且这么做也有些不合理,有没有简单的方法能够触发这些控件的Validated事件呢?

 

在网上找了找,发现解决办法其实很简单,Form类提供了一个ValidateChildren方法,使用它就可以轻松地实现上述需求


 

MSDN上的资料 http://msdn.microsoft.com/en-us/library/ms159409.aspx

.NET Framework Class Library
Form.ValidateChildren Method

 

This member overrides ContainerControl.ValidateChildren(), and more complete documentation might be available in that topic.

Causes all of the child controls within a control that support validation to validate their data.

 

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

C#
[BrowsableAttribute( true)]
public override bool ValidateChildren()
Return Value
Type: System.Boolean
true if all of the children validated successfully; otherwise, false. If called from the Validating or Validated event handlers, this method will always return false.
Platforms

 

 

如果不想验证所有的控件,比如有些控件可能Visible属性会随时发生变化,在验证时想跳过这些控件,方法就是在调用ValidateChildren时传递参数进行限制

public virtual bool ValidateChildren (
    ValidationConstraints validationConstraints
)

 

 

ValidationConstraints Enumeration Members

 Member nameDescription
EnabledValidates child controls whose Enabled property is set to true
ImmediateChildrenValidates child controls that are directly hosted within the container. Does not validate any of the children of these children. For example, if you have a Form that contains a custom UserControl, and the UserControl contains a Button, using ImmediateChildren will cause the Validating event of the UserControl to occur, but not the Validating event of the Button.  
NoneValidates all child controls, and all children of these child controls, regardless of their property settings.  
SelectableValidates child controls that can be selected. 
TabStopValidates child controls that have a TabStop value set, which means that the user can navigate to the control using the TAB key.  
VisibleValidates child controls whose Visible property is set to true

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值