ContainerControl.ValidateChildren 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使控件内支持验证的所有子控件都对其数据进行验证。
重载
ValidateChildren() |
使控件内支持验证的所有子控件都对其数据进行验证。 |
ValidateChildren(ValidationConstraints) |
使控件内支持验证的所有子控件都对其数据进行验证。 |
ValidateChildren()
使控件内支持验证的所有子控件都对其数据进行验证。
public:
virtual bool ValidateChildren();
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren ();
[<System.ComponentModel.Browsable(false)>]
abstract member ValidateChildren : unit -> bool
override this.ValidateChildren : unit -> bool
Public Overridable Function ValidateChildren () As Boolean
返回
如果成功验证所有子级,则为 true
;否则为 false
。 如果是从 Validating 或 Validated 事件处理程序调用的,则该方法将始终返回 false
。
- 属性
示例
下面的代码示例关闭对窗体及其所有包含控件的隐式验证,而是在单击鼠标按钮时手动执行窗体的所有子项的验证。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
namespace TestValidation
{
class Form1 : Form
{
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
private TextBox firstNameBox, lastNameBox;
private Button validateButton;
private FlowLayoutPanel flowLayout1;
private Form1()
{
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
// Turn off validation when a control loses focus. This will be inherited by child
// controls on the form, enabling us to validate the entire form when the
// button is clicked instead of one control at a time.
this.AutoValidate = AutoValidate.Disable;
flowLayout1 = new FlowLayoutPanel();
flowLayout1.Dock = DockStyle.Fill;
flowLayout1.Name = "flowLayout1";
firstNameBox = new TextBox();
firstNameBox.Name = "firstNameBox";
firstNameBox.Size = new Size(75, firstNameBox.Size.Height);
firstNameBox.CausesValidation = true;
firstNameBox.Validating += new System.ComponentModel.CancelEventHandler(firstNameBox_Validating);
flowLayout1.Controls.Add(firstNameBox);
lastNameBox = new TextBox();
lastNameBox.Name = "lastNameBox";
lastNameBox.Size = new Size(75, lastNameBox.Size.Height);
lastNameBox.CausesValidation = true;
lastNameBox.Validating += new System.ComponentModel.CancelEventHandler(lastNameBox_Validating);
flowLayout1.Controls.Add(lastNameBox);
validateButton = new Button();
validateButton.Text = "Validate";
// validateButton.Location = new Point(170, 10);
validateButton.Size = new Size(75, validateButton.Size.Height);
validateButton.Click += new EventHandler(validateButton_Click);
flowLayout1.Controls.Add(validateButton);
this.Controls.Add(flowLayout1);
this.Text = "Test Validation";
}
void firstNameBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (firstNameBox.Text.Length == 0)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
void lastNameBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = false;
}
void validateButton_Click(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
MessageBox.Show("Validation succeeded!");
}
else
{
MessageBox.Show("Validation failed.");
}
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Private Overloads Shared Sub Main(ByVal args() As String)
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Private WithEvents FirstNameBox, LastNameBox As TextBox
Private WithEvents ValidateButton As Button
Private FlowLayout1 As FlowLayoutPanel
Private Sub New()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Turn off validation when a control loses focus. This will be inherited by child
' controls on the form, enabling us to validate the entire form when the
' button is clicked instead of one control at a time.
Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
FlowLayout1 = New FlowLayoutPanel()
FlowLayout1.Dock = DockStyle.Fill
FirstNameBox = New TextBox()
FirstNameBox.Name = "FirstNameBox"
FirstNameBox.Location = New Point(10, 10)
FirstNameBox.Size = New Size(75, FirstNameBox.Size.Height)
FirstNameBox.CausesValidation = True
FlowLayout1.Controls.Add(FirstNameBox)
LastNameBox = New TextBox()
LastNameBox.Name = "LastNameBox"
LastNameBox.Location = New Point(90, 10)
LastNameBox.Size = New Size(75, LastNameBox.Size.Height)
LastNameBox.CausesValidation = True
FlowLayout1.Controls.Add(LastNameBox)
ValidateButton = New Button()
ValidateButton.Text = "Validate"
ValidateButton.Location = New Point(170, 10)
ValidateButton.Size = New Size(75, ValidateButton.Size.Height)
FlowLayout1.Controls.Add(ValidateButton)
Me.Text = "Test Validation"
Me.Controls.Add(FlowLayout1)
End Sub
Private Sub FirstNameBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FirstNameBox.Validating
If FirstNameBox.Text.Length = 0 Then
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub LastNameBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles LastNameBox.Validating
e.Cancel = False
End Sub
Private Sub ValidateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ValidateButton.Click
If ValidateChildren() Then
MessageBox.Show("Validation succeeded!")
Else
MessageBox.Show("Validation failed.")
End If
End Sub
End Class
注解
ValidateChildren 将降序控件的层次结构,并检查每个控件以查看它是否支持验证。 如果用户可以选择控件,并且其 CausesValidation 属性为 true
, ValidateChildren 将导致 Validating 事件发生。 如果任何控件取消事件 Validating ,此方法将返回 false
;否则,它将返回 true
。
如果控件绑定到数据源,并且 Validating 事件发生,则会导致控件将其当前数据推送回数据源。
调用 ValidateChildren 等效于ValidateChildren使用 的 None调用ValidationConstraints。
适用于
ValidateChildren(ValidationConstraints)
使控件内支持验证的所有子控件都对其数据进行验证。
public:
virtual bool ValidateChildren(System::Windows::Forms::ValidationConstraints validationConstraints);
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren (System.Windows.Forms.ValidationConstraints validationConstraints);
[<System.ComponentModel.Browsable(false)>]
abstract member ValidateChildren : System.Windows.Forms.ValidationConstraints -> bool
override this.ValidateChildren : System.Windows.Forms.ValidationConstraints -> bool
Public Overridable Function ValidateChildren (validationConstraints As ValidationConstraints) As Boolean
参数
- validationConstraints
- ValidationConstraints
添加对哪些控件可以引发其 Validating 事件的限制。
返回
如果成功验证所有子级,则为 true
;否则为 false
。 如果是从 Validating 或 Validated 事件处理程序调用的,则该方法将始终返回 false
。
- 属性
示例
下面的代码示例仅导致Validating其 属性true
为 的窗体Enabled的直接子级发生事件。
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace ValidateChildrenWithConstraints
{
class Form1 : Form
{
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
private Form1()
{
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
// Create controls on form.
TextBox textBox1, textBox2, textBox3;
FlowLayoutPanel flowPanel1;
TextBox subTextBox1;
Button button1;
this.Size = new Size(500, 300);
this.AutoValidate = AutoValidate.Disable;
textBox1 = new TextBox();
textBox1.Location = new Point(20, 20);
textBox1.Size = new Size(75, textBox1.Size.Height);
textBox1.CausesValidation = true;
textBox1.Validating += new System.ComponentModel.CancelEventHandler(textBox1_Validating);
this.Controls.Add(textBox1);
textBox2 = new TextBox();
textBox2.Location = new Point(105, 20);
textBox2.Size = new Size(75, textBox2.Size.Height);
textBox2.CausesValidation = true;
textBox2.Validating += new System.ComponentModel.CancelEventHandler(textBox2_Validating);
this.Controls.Add(textBox2);
textBox3 = new TextBox();
textBox3.Location = new Point(190, 20);
textBox3.Size = new Size(75, textBox3.Size.Height);
textBox3.Enabled = false;
textBox3.CausesValidation = true;
textBox3.Validating += new System.ComponentModel.CancelEventHandler(textBox3_Validating);
this.Controls.Add(textBox3);
button1 = new Button();
button1.Text = "Click";
button1.Location = new Point(270, 20);
button1.Click += new EventHandler(button1_Click);
this.Controls.Add(button1);
flowPanel1 = new FlowLayoutPanel();
flowPanel1.Size = new Size(400, 100);
flowPanel1.Dock = DockStyle.Bottom;
subTextBox1 = new TextBox();
subTextBox1.CausesValidation = true;
subTextBox1.Validating += new System.ComponentModel.CancelEventHandler(subTextBox1_Validating);
flowPanel1.Controls.Add(subTextBox1);
this.Controls.Add(flowPanel1);
}
void subTextBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("subTextBox1 Validating!");
}
void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("textBox1 Validating!");
}
void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("textBox2 Validating!");
}
void textBox3_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("textBox3 Validating!");
}
void button1_Click(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.ImmediateChildren | ValidationConstraints.Enabled);
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Namespace ValidateChildrenWithConstraints
_
Class Form1
Inherits Form
Public Overloads Shared Sub Main(ByVal args() As String)
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Private Sub New()
AddHandler Me.Load, AddressOf Form1_Load
End Sub
Dim WithEvents TextBox1, TextBox2, TextBox3 As TextBox
Dim FlowPanel1 As FlowLayoutPanel
Dim WithEvents SubTextBox1 As TextBox
Dim WithEvents Button1 As Button
Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create controls on form.
Me.Size = New Size(500, 300)
Me.AutoValidate = AutoValidate.Disable
TextBox1 = New TextBox()
TextBox1.Location = New Point(20, 20)
TextBox1.Size = New Size(75, TextBox1.Size.Height)
TextBox1.CausesValidation = True
Me.Controls.Add(TextBox1)
TextBox2 = New TextBox()
TextBox2.Location = New Point(105, 20)
TextBox2.Size = New Size(75, TextBox2.Size.Height)
TextBox2.CausesValidation = True
Me.Controls.Add(TextBox2)
TextBox3 = New TextBox()
TextBox3.Location = New Point(190, 20)
TextBox3.Size = New Size(75, TextBox3.Size.Height)
TextBox3.Enabled = False
TextBox3.CausesValidation = True
Me.Controls.Add(TextBox3)
Button1 = New Button()
Button1.Text = "Click"
Button1.Location = New Point(270, 20)
Me.Controls.Add(Button1)
FlowPanel1 = New FlowLayoutPanel()
FlowPanel1.Size = New Size(400, 100)
FlowPanel1.Dock = DockStyle.Bottom
SubTextBox1 = New TextBox()
SubTextBox1.CausesValidation = True
FlowPanel1.Controls.Add(SubTextBox1)
Me.Controls.Add(FlowPanel1)
End Sub
Sub SubTextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SubTextBox1.Validating
MessageBox.Show("SubTextBox1 Validating!")
End Sub
Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
MessageBox.Show("TextBox1 Validating!")
End Sub
Sub TextBox2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
MessageBox.Show("TextBox2 Validating!")
End Sub
Sub TextBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox3.Validating
MessageBox.Show("TextBox3 Validating!")
End Sub
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Me.ValidateChildren((ValidationConstraints.ImmediateChildren Or ValidationConstraints.Enabled))
End Sub
End Class
End Namespace 'ValidateChildrenWithConstraints
注解
ValidateChildren将检查当前控件的所有子控件,如果控件满足 拼写出ValidationConstraints的条件,则会导致Validating该事件在 控件上发生。
通过将多个参数与按位 OR 运算符组合,可以一次使用多个 ValidationConstraints 参数。 将参数与按位 OR 运算符组合将导致逻辑 AND 运算。 例如, 调用 ValidateChildren(ValidationConstraints.ImmediateChildren | ValidationConstraints.Enabled)
将仅 Validating 引发同时是容器和 的直接子级的控件上的 事件。
无论为此方法指定哪些参数,控件的 属性都必须 CausesValidation 设置为 true
,才能发生其 Validating 事件。 如果希望仅在调用 ValidateChildren时进行验证,而不是当用户从控件中转移焦点时进行验证,则还应将 控件或控件容器false
的 属性设置为 AutoValidate 。
如果控件绑定到数据源,并且 Validating 事件发生,则会导致控件将其当前数据推送回数据源。
不能通过应用按位求反运算符来实现参数的 ValidationConstraints 相反效果。 例如,如果将 字段的 Visible 负值提供给 ValidateChildren,则它不会验证容器上不可见的所有子级。 向 提供任何负参数 ValidateChildren 不会影响容器或其子级。