ContainerControl.ValidateChildren Method

Definition

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

Overloads

ValidateChildren()

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

ValidateChildren(ValidationConstraints)

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

ValidateChildren()

Source:
ContainerControl.cs
Source:
ContainerControl.cs
Source:
ContainerControl.cs

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

C#
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren();

Returns

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.

Attributes

Examples

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.

C#
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.");
            }
        }
    }
}

Remarks

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

ValidateChildren(ValidationConstraints)

Source:
ContainerControl.cs
Source:
ContainerControl.cs
Source:
ContainerControl.cs

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

C#
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren(System.Windows.Forms.ValidationConstraints validationConstraints);

Parameters

validationConstraints
ValidationConstraints

Places restrictions on which controls have their Validating event raised.

Returns

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.

Attributes

Examples

The following code example will only cause the Validating event to occur for immediate children of the form whose Enabled property is true.

C#
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);
        }
    }
}

Remarks

ValidateChildren will examine all the children of the current control, causing the Validating event to occur on a control if it meets the criteria spelled out by ValidationConstraints.

You may use several ValidationConstraints parameters at once by combining them with a bitwise OR operator. Combining parameters with a bitwise OR operator will result in a logical AND operation. For example, calling ValidateChildren(ValidationConstraints.ImmediateChildren | ValidationConstraints.Enabled) will only raise the Validating event on controls that are both immediate children of the container AND are enabled.

Regardless of which parameters you specify for this method, a control must have its CausesValidation property set to true in order for its Validating event to occur. You should also set the AutoValidate property of the control or of the control's container to false if you want validation to happen only when you call ValidateChildren, and not when the user shifts focus from the control.

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.

You cannot achieve the opposite effect of a ValidationConstraints parameter by applying a bitwise negation operator. For example, if you supply the negative value of the Visible field to ValidateChildren, it will not validate all children that are not visible on the container. Supplying any negative parameter to ValidateChildren will have no effect on the container or its children.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9