ValidationConstraints 枚举

定义

定义一些常数,用于通知 ValidateChildren(ValidationConstraints) 应如何验证容器的子控件。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum ValidationConstraints
继承
ValidationConstraints
属性

字段

名称 说明
Enabled 2

验证其 Enabled 属性设置为 true 的子控件。

ImmediateChildren 16

验证容器直接承载的子控件。 不验证这些子控件的任何子控件。 例如,如果有一个 Form,其中包含一个自定义 UserControl,并且该 UserControl 包含一个 Button,则使用 ImmediateChildren 会引发 ValidatingUserControl 事件,而不会引发 ValidatingButton 事件。

None 0

验证所有子控件和这些子控件的所有子控件,无论其属性如何设置。

Selectable 1

验证可以选择的子控件。

TabStop 8

验证设置了 TabStop 值的子控件,设置该值意味着用户可以使用 Tab 键导航到控件。

Visible 4

验证其 Visible 属性设置为 true 的子控件。

示例

下面的代码示例将只导致Validating针对其 属性为 true的窗体Enabled的直接子级引发 事件。

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

注解

默认情况下, ValidateChildren 将验证容器(如窗体)中所有已启用的控件。 使用此枚举来限制其事件被引发的 Validating 控件的类型。

可以将这些枚举值与按位 OR 运算组合在一起。 将参数与按位 OR 运算符组合将导致逻辑 AND 运算。 例如, 调用 ValidateChildren(ValidationConstraints.ImmediateChildren | ValidationConstraints.Enabled) 将仅 Validating 引发同时是容器和 的直接子级的控件上的 事件。

如果在调用 ValidateChildren时未指定 ImmediateChildren, 方法将要求验证控件层次结构中的所有子控件。

适用于

产品 版本
.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

另请参阅