0

Yesterday I have implemented some validating events for controls in a groupbox located on a WinForm. I have set the AutoValidate property of the form to Disabled, I have set the CausesValidation property of the controls to true and implemented the Validating event of the controls. By calling the ValidateChildren() method of the form I force that validating events are executed. This was working all fine.

But after placing this groupbox on top of a picturebox and setting the picturebox as parent of the groupbox then validating events aren't executed anymore....

Below some demo code. Form is only containing a picturebox, groupbox, textbox and button.

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;
    }
}
Doki
  • 11
  • 2

1 Answers1

1

The ValidateChildren() method calls ValidateChildren(ValidationConstraints.Selectable) to get the job done. That's a problem for a PictureBox, it isn't selectable. So none of its children get validated either.

Calling it with ValidationConstraints.None doesn't work either, the ability to validate child controls is implemented by ContainerControl and PictureBox doesn't derive from it. So you can't call ValidateChildren on the PictureBox either. Enumerating the controls yourself and triggering the Validating event can't work either, the PerformControlValidation() method is internal.

You'll need to re-think the idea of trying to turn the PictureBox into a ContainerControl. Most any control can resemble a picturebox, if not through the BackgroundImage property then through the Paint event.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • No, if you don't add any parameters then by default ValidationConstraints.None is used. I also have checked this by adding this constraint to the ValidateChildren method, but without any successfull result. – Doki Aug 01 '12 at 10:51
  • I use the picturebox to get a transparent groupbox. Because this isn't possible via the BackgroundColor property (set color = transparent) I use a picturebox, then I don't have to customize the Paint event. But result of this is that validation via ValidateChildren() isn't working anymore. Is there no alternative solution than customizing the Paint event? – Doki Aug 01 '12 at 12:46
  • I have tested this, that's indeed working. But that means that I have to create a user controls for each form of my application and move all the controls and logic from the forms to the user controls... – Doki Aug 01 '12 at 13:53
  • Nothing a cut-and-paste couldn't solve. You can turn any form into a user control by setting its TopLevel property to false. This is veering off topic, click Ask Question if you want to know more about it. – Hans Passant Aug 01 '12 at 13:58