0

I have an errorProvider control. I would like to use it to validate if a listbox contains at least an element.

Here's my code to register the validating event:

this.selectedFieldsArea.Validating +=new CancelEventHandler(selectedFieldsArea_Validating);

And here is my validating function:

private void selectedFieldsArea_Validating(object sender, CancelEventArgs e)
{
    if (this.selectedFieldsArea.Items.Count == 0)
    {
        errorProvider1.SetError(selectedFieldsArea, "Need to select at least 1 field");
        e.Cancel = true;
    }
    else
        errorProvider1.SetError(selectedFieldsArea, "");
}

Although I registered the method it is never called. Have I forgotten to do something?

nche
  • 1,082
  • 3
  • 14
  • 32
  • Oops, just found out I have to call ValidateChildren() method on my form for the event to be launched. Still learning. Thanks anyways – nche Nov 24 '11 at 13:57

1 Answers1

0

In order for the _validating event to be fired, the Validate() or ValidateChildren() method has to be called. I added this in my button_Click() event and it now works.

nche
  • 1,082
  • 3
  • 14
  • 32