分享

ContainerControl.ValidateChildren 方法 (System.Windows.Forms)

 昵称42436196 2017-04-28

使控件内支持验证的所有子控件都对其数据进行验证。

命名空间:   System.Windows.Forms
程序集:  System.Windows.Forms(位于 System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public virtual bool ValidateChildren()

返回值

Type: System.Boolean

如果成功验证所有子级,则为 true;否则为 false 如果是从 ValidatingValidated 事件处理程序调用的,则该方法将始终返回 false

ValidateChildren 将向下查看到控件的层次结构,并检查每个控件,以确定它是否支持验证。 如果可以由用户选择该控件并将其 CausesValidation 属性是 true, ,ValidateChildren 将导致 Validating 事件发生。 如果任一控件取消 Validating 事件时,此方法将返回 false; 否则为它将返回 true

如果控件绑定到数据源时,与 Validating 事件发生时,它将导致要将其当前数据推送回数据源的控件。

调用 ValidateChildren 等效于调用 ValidateChildrenValidationConstraintsNone

下面的代码示例将关闭隐式验证窗体和所有其包含的控件,并改为在单击鼠标按钮时手动执行验证的所有窗体的子级。

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多