繁体   English   中英

未为PictureBox的子控件触发验证事件

[英]Validating event not fired for child controls of picturebox

昨天,我已经在WinForm上的组框中实现了一些控件的验证事件。 我已将窗体的AutoValidate属性设置为Disabled,已将控件的CausesValidation属性设置为true并实现了控件的Validating事件。 通过调用以下形式的ValidateChildren()方法,我强制执行了验证事件。 一切正常。

但是在将此组框放置在图片框顶部并将该图片框设置为该组框的父级之后,不再执行验证事件...。

下面是一些演示代码。 表单仅包含图片框,组框,文本框和按钮。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        MessageBox.Show("Validating textbox");
        e.Cancel = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (ValidateChildren())
            MessageBox.Show("Validation not executed :-(");
        else
            MessageBox.Show("Validation executed :-)");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        groupBox1.Parent = pictureBox1;
    }
}

ValidateChildren()方法调用ValidateChildren(ValidationConstraints.Selectable)来完成工作。 对于PictureBox来说,这是一个问题,它是无法选择的。 因此,它的所有子级都没有得到验证。

使用ValidationConstraints调用它也无效,ContainerControl实现了验证子控件的能力,PictureBox也不能从中派生。 因此,您也无法在PictureBox上调用ValidateChildren。 自己枚举控件并触发Validating事件也不起作用,PerformControlValidation()方法是内部的。

您需要重新考虑尝试将PictureBox转换为ContainerControl的想法。 如果不通过BackgroundImage属性,然后通过Paint事件,则大多数任何控件都可以类似于图片框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM