ContainerControl.ValidateChildren Method ()
Causes all of the child controls within a control that support validation to validate their data.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Return Value
Type: System.Booleantrue if all of the children validated successfully; otherwise, false. If called from the Validating or Validated event handlers, this method will always return false.
ValidateChildren will descend a control's hierarchy and examine each control to see if it supports validation. If the control can be selected by the user and its CausesValidation property is true, ValidateChildren will cause the Validating event to occur. If any of the controls cancel the Validating event, this method will return false; otherwise, it will return true.
If a control is bound to a data source, and the Validating event occurs, it will cause the control to push its current data back to the data source.
Calling ValidateChildren is equivalent to calling ValidateChildren with a ValidationConstraints of None.
The following code example turns off implicit validation for a form and all of its contained controls, and instead manually performs validation of all of the form's children when a mouse button is clicked.
Imports System 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 'Main 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
Available since 2.0