閱讀英文

共用方式為


ValidationConstraints 列舉

定義

定義常數,以便告知 ValidateChildren(ValidationConstraints) 它應該如何驗證容器的子控制項。

此列舉支援其成員值的位元組合。

C#
[System.Flags]
public enum ValidationConstraints
繼承
ValidationConstraints
屬性

欄位

名稱 Description
Enabled 2

驗證 Enabled 屬性設定為 true 的子控制項。

ImmediateChildren 16

驗證直接裝載於容器內的子控制項。 但不驗證這些子系的任何子系。 例如,如果您有一個含有自訂 FormUserControl,而且 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) 只會在同時啟用容器 AND 子系的控制項上引發 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

另請參閱