ValidationConstraints ValidationConstraints ValidationConstraints ValidationConstraints Enum

定义

定义一些常数,用于通知 ValidateChildren(ValidationConstraints) 应如何验证容器的子控件。Defines constants that inform ValidateChildren(ValidationConstraints) about how it should validate a container's child controls.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class ValidationConstraints
[System.Flags]
public enum ValidationConstraints
type ValidationConstraints = 
Public Enum ValidationConstraints
继承
ValidationConstraintsValidationConstraintsValidationConstraintsValidationConstraints
属性

字段

Enabled Enabled Enabled Enabled 2

验证其 Enabled 属性设置为 true 的子控件。Validates child controls whose Enabled property is set to true.

ImmediateChildren ImmediateChildren ImmediateChildren ImmediateChildren 16

验证容器直接承载的子控件。Validates child controls that are directly hosted within the container. 不验证这些子控件的任何子控件。Does not validate any of the children of these children. 例如,如果有一个 Form,其中包含一个自定义 UserControl,并且该 UserControl 包含一个 Button,则使用 ImmediateChildren 会引发 ValidatingUserControl 事件,而不会引发 ValidatingButton 事件。For example, if you have a Form that contains a custom UserControl, and the UserControl contains a Button, using ImmediateChildren will cause the Validating event of the UserControl to occur, but not the Validating event of the Button.

None None None None 0

验证所有子控件和这些子控件的所有子控件,无论其属性如何设置。Validates all child controls, and all children of these child controls, regardless of their property settings.

Selectable Selectable Selectable Selectable 1

验证可以选择的子控件。Validates child controls that can be selected.

TabStop TabStop TabStop TabStop 8

验证设置了 TabStop 值的子控件,设置该值意味着用户可以使用 Tab 键导航到控件。Validates child controls that have a TabStop value set, which means that the user can navigate to the control using the TAB key.

Visible Visible Visible Visible 4

验证其 Visible 属性设置为 true 的子控件。Validates child controls whose Visible property is set to true.

示例

下面的代码示例将仅Validating为其Enabled属性为true的窗体的直接子级引发事件。The following code example will only cause the Validating event to be raised 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 , 将验证容器中所有已启用的控件, 如窗体。By default, ValidateChildren will validate all enabled controls in a container, such as a form. 使用此枚举可限制引发Validating事件的控件类型。Use this enumeration to restrict the types of controls whose Validating event is raised.

可以将这些枚举值与按位 "或" 运算组合在一起。You can combine these enumerated values together with a bitwise OR operation. 使用按位 "或" 运算符组合参数将导致逻辑 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.

如果在调用ValidateChildren时没有指定 ImmediateChildren, 则该方法将要求您验证控件层次结构中的所有子控件。If you do not specify ImmediateChildren when you call ValidateChildren, the method will require that you validate all child controls in the control hierarchy.

适用于

另请参阅