ContainerControl.ValidateChildren ContainerControl.ValidateChildren ContainerControl.ValidateChildren ContainerControl.ValidateChildren Method

정의

컨트롤 내에서 유효성 검사를 지원하는 모든 자식 컨트롤이 해당 데이터의 유효성을 검사하도록 합니다.Causes all of the child controls within a control that support validation to validate their data.

오버로드

ValidateChildren() ValidateChildren() ValidateChildren() ValidateChildren()

컨트롤 내에서 유효성 검사를 지원하는 모든 자식 컨트롤이 해당 데이터의 유효성을 검사하도록 합니다.Causes all of the child controls within a control that support validation to validate their data.

ValidateChildren(ValidationConstraints) ValidateChildren(ValidationConstraints) ValidateChildren(ValidationConstraints)

컨트롤 내에서 유효성 검사를 지원하는 모든 자식 컨트롤이 해당 데이터의 유효성을 검사하도록 합니다.Causes all of the child controls within a control that support validation to validate their data.

ValidateChildren() ValidateChildren() ValidateChildren() ValidateChildren()

컨트롤 내에서 유효성 검사를 지원하는 모든 자식 컨트롤이 해당 데이터의 유효성을 검사하도록 합니다.Causes all of the child controls within a control that support validation to validate their data.

public:
 virtual bool ValidateChildren();
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren ();
abstract member ValidateChildren : unit -> bool
override this.ValidateChildren : unit -> bool
Public Overridable Function ValidateChildren () As Boolean

반환

모든 자식의 유효성을 검사했으면 true이고, 검사하지 못했으면 false입니다.true if all of the children validated successfully; otherwise, false. 이 메서드는 Validating 또는 Validated 이벤트 처리기에서 호출된 경우 항상 false를 반환합니다.If called from the Validating or Validated event handlers, this method will always return false.

예제

다음 코드 예제는 양식 및 포함된 된 컨트롤의 모든 암시적 유효성 검사가 해제 하 고 마우스 단추를 클릭할 때 대신 유효성 검사의 모든 폼의 자식을 수동으로 수행 합니다.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.

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 컨트롤의 계층 구조를 내림차순를 각 컨트롤에 유효성 검사를 지원 하는지 검사 합니다.ValidateChildren will descend a control's hierarchy and examine each control to see if it supports validation. 사용자가 컨트롤을 선택할 수 있으면 및 해당 CausesValidation 속성은 trueValidateChildren 하면는 Validating 이벤트를 발생 합니다.If the control can be selected by the user and its CausesValidation property is true, ValidateChildren will cause the Validating event to occur. 컨트롤 중 하나를 취소 합니다 Validating 경우가이 메서드는 반환 false이 고, 그렇지 않으면 반환 됩니다 true합니다.If any of the controls cancel the Validating event, this method will return false; otherwise, it will return true.

컨트롤을 데이터 원본에 바인딩되는 경우 및 Validating 컨트롤에서 현재 데이터를 데이터 원본에 다시 푸시 하면 이벤트가 발생 합니다.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.

호출 ValidateChildren 호출과 같습니다 ValidateChildren 사용 하 여를 ValidationConstraintsNone합니다.Calling ValidateChildren is equivalent to calling ValidateChildren with a ValidationConstraints of None.

ValidateChildren(ValidationConstraints) ValidateChildren(ValidationConstraints) ValidateChildren(ValidationConstraints)

컨트롤 내에서 유효성 검사를 지원하는 모든 자식 컨트롤이 해당 데이터의 유효성을 검사하도록 합니다.Causes all of the child controls within a control that support validation to validate their data.

public:
 virtual bool ValidateChildren(System::Windows::Forms::ValidationConstraints validationConstraints);
[System.ComponentModel.Browsable(false)]
public virtual bool ValidateChildren (System.Windows.Forms.ValidationConstraints validationConstraints);
abstract member ValidateChildren : System.Windows.Forms.ValidationConstraints -> bool
override this.ValidateChildren : System.Windows.Forms.ValidationConstraints -> bool

매개 변수

validationConstraints
ValidationConstraints ValidationConstraints ValidationConstraints ValidationConstraints

Validating 이벤트가 발생한 컨트롤을 제한합니다.Places restrictions on which controls have their Validating event raised.

반환

모든 자식의 유효성을 검사했으면 true이고, 검사하지 못했으면 false입니다.true if all of the children validated successfully; otherwise, false. 이 메서드는 Validating 또는 Validated 이벤트 처리기에서 호출된 경우 항상 false를 반환합니다.If called from the Validating or Validated event handlers, this method will always return false.

예제

다음 코드 예제에서는 하면 합니다 Validating 폼의 직계 자식을 갖는 발생할 이벤트 Enabled 속성은 true합니다.The following code example will only cause the Validating event to occur for immediate children of the form whose Enabled property is true.

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 현재 컨트롤의 모든 자식 항목을 검토 하는 발생 합니다 Validating 여 명시 된 조건을 충족 하는 경우 컨트롤에서 발생 하는 이벤트 ValidationConstraints합니다.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.

몇 가지를 사용할 수 있습니다 ValidationConstraints 비트 OR 연산자를 결합 하 여 한 번에 매개 변수입니다.You may use several ValidationConstraints parameters at once by combining them with a bitwise OR operator. 비트 OR 연산자를 사용 하 여 매개 변수를 결합 하면 논리적 AND 연산을 줄어듭니다.Combining parameters with a bitwise OR operator will result in a logical AND operation. 예를 들어, 호출 ValidateChildren(ValidationConstraints.ImmediateChildren | ValidationConstraints.Enabled) 만 발생 시킵니다는 Validating 컨테이너의 직계 자식이 고 사용 하는 컨트롤의 이벤트입니다.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.

이 메서드에 대해 지정할 매개 변수에 관계 없이 컨트롤 있어야 해당 CausesValidation 속성으로 설정 true 되려면에서 해당 Validating 이벤트를 발생 합니다.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. 설정 해야 합니다 AutoValidate 컨트롤 또는 컨트롤의 컨테이너의 속성 false 유효성 검사를 호출할 때만 수행 하려는 경우 ValidateChildren, 되지 경우 사용자에서에서 포커스를 이동할 컨트롤 및 합니다.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.

컨트롤을 데이터 원본에 바인딩되는 경우 및 Validating 컨트롤에서 현재 데이터를 데이터 원본에 다시 푸시 하면 이벤트가 발생 합니다.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.

반대 효과 얻을 수 없습니다는 ValidationConstraints 비트 부정 연산자를 적용 하 여 매개 변수입니다.You cannot achieve the opposite effect of a ValidationConstraints parameter by applying a bitwise negation operator. 예를 들어의 음수 값을 제공 하는 경우는 Visible 필드를 ValidateChildren, 컨테이너에 표시 되지 않는 모든 자식을 검사 하지 것입니다.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. 모든 음수 매개 변수를 제공 ValidateChildren 컨테이너 또는 해당 자식에 영향을 주지 것입니다.Supplying any negative parameter to ValidateChildren will have no effect on the container or its children.

적용 대상