Export (0) Print
Expand All

ContainerControl.ValidateChildren Method

Updated: August 2008

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)

'Declaration
<BrowsableAttribute(False)> _
Public Overridable Function ValidateChildren As Boolean
'Usage
Dim instance As ContainerControl 
Dim returnValue As Boolean 

returnValue = instance.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.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

August 2008

Fixed reference so that code example appears.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft