Export (0) Print
Expand All

ContainerControl.AutoValidate Property

Gets or sets a value that indicates whether controls in this container will be automatically validated when the focus changes.

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

'Declaration
<BrowsableAttribute(False)> _
Public Overridable Property AutoValidate As AutoValidate

Property Value

Type: System.Windows.Forms.AutoValidate
An AutoValidate enumerated value that indicates whether contained controls are implicitly validated on focus change. The default is Inherit.

ExceptionCondition
InvalidEnumArgumentException

A AutoValidate value that is not valid was used to set this property.

The default value for AutoValidate will differ for each control. For more details on implicit validation, see User Input Validation in Windows Forms.

Setting this property to a new value will raise the AutoValidateChanged event.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft