英語で読む

次の方法で共有


ValidationConstraints 列挙型

定義

コンテナーの子コントロールの検証方法を ValidateChildren(ValidationConstraints) に通知する定数を定義します。

この列挙体は、メンバー値のビットごとの組み合わせをサポートしています。

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

フィールド

名前 説明
Enabled 2

Enabled プロパティが true に設定されている子コントロールを検証します。

ImmediateChildren 16

コンテナー内に直接ホストされている子コントロールを検証します。 これらの子コントロールの子は検証されません。 たとえば、カスタムの Form を含む UserControl があり、その UserControlButton が含まれている場合、ImmediateChildren を使用すると、ValidatingUserControl イベントは発生しますが、ValidatingButton イベントは発生しません。

None 0

プロパティの設定にかかわらず、すべての子コントロールと、その子コントロールのすべての子を検証します。

Selectable 1

選択できる子コントロールを検証します。

TabStop 8

TabStop の値が設定されている (つまりユーザーが Tab キーを使用して移動できる) 子コントロールを検証します。

Visible 4

Visible プロパティが true に設定されている子コントロールを検証します。

次のコード例では、 プロパティが Validating のフォームの直接の子に対してのみ イベントがEnabledtrue発生します。

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 し、AND が有効になります。

を呼び出 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

こちらもご覧ください