views:

131

answers:

5

I am just getting to grips with the concept of a UserControl.

I've created a UserControl to group together a number of controls that were being duplicated on individual pages of a TabControl.

Some of these controls are text fields that require validation, and when validation is unsuccessful I need to display an error message. However the place where I want to display the error message is on the status bar on the main form.

What is the best way to handle validation/error display in this situation?

A: 

You can either put a validator on the user control itself, throw an exception, or add public getters to the fields you want shown in the parent form.

Chris Doggett
@Crhis throw an exception?
eglasius
Indeed. An exception would be a bit ewwwwwwwwww.
Quibblesome
@Quarrelsome not only that, if the user control is validating on load or any other page event, it would actually fail the whole page. -1
eglasius
A: 

Make a public method on your usercontrol that validates its field, and you can pass in a string output parameter.

so something like

    public bool IsValid(out string status)
{
 // do validation and set the status message
}
Brandon
+4  A: 

To handle validation do one of these:

  • Validate with a method inside the user control
  • Have your user control have a delegate property (e.g. ValidationHandler) that can handle the validation (this would allow you to have a class with a bunch of validators that you could assign to your controls)

    public delegate void Validator(...)

    public Validator ValidationHandler { get; set; }

  • Have your user control generate a validation request event (e.g. ValidationRequested)

    public event EventHandler<ValidationEventArgs> ValidationRequested

To notify the system that an error has occurred do one of these:

  • Use an event that interested parties can subscribe to (e.g. ValidationFailed)

  • If the object that performs the validation (via the delegate or event) is also the one that you want to generate the error message from, it can raise the error message itself.

EDIT:

Since you've said you would validate inside your control, the code for a ValidationFailed event might look like:

// In your user control

public class ValidationFailedEventArgs : EventArgs
{
   public ValidationFailedEventArgs(string message)
   {
      this.Message = message;
   }

   public string Message { get; set; }
}

private EventHandler<ValidationFailedEventArgs> _validationFailed;
public event EventHandler<ValidationFailedEventArgs> ValidationFailed
{
   add { _validationFailed += value; }
   remove { _validationFailed -= value; }
}

protected void OnValidationFailed(ValidationFailedEventArgs e)
{
   if(_validationFailed != null)
      _validationFailed(this, e);
}

private void YourValidator()
{
   if(!valid)
   {
      ValidationFailedEventArgs args = 
         new ValidationFailedEventArgs("Your Message");
      OnValidationFailed(args);
   }
}

// In your main form:

userControl.ValidationFailed += 
   new EventHandler<ValidationFailedEventArgs>(userControl_ValidationFailed);

// ...
private void userControl_ValidationFailed(object sender, 
                                          ValidationFailedEventArgs e)
{
   statusBar.Text = e.Message;
}
Daniel LeCheminant
+1 I really like this answer. I do think it is a bit involved, a link to a reference implementation/article or something would be great to get it going
eglasius
I am validating from within the usercontrol - which works just fine. I just need to get the error message displayed on the main form.Can you point me in the right direction for how to define an event and handle the subscription?
phrenetic
Use a ValidationFailed event, the main form can handle the event
Daniel LeCheminant
@Daniel when I read your initial answer, I thought you were going for something like a common validation class that all parties used, which would be nice so you don't have to subscribe to each control event in all the places the control is used. I go for asp.net validators then :)
eglasius
@Freddy: Well, that's what I'd do (and that was one of my suggestions) ... but the OP wants to validate in the control, so I'm focusing on that...
Daniel LeCheminant
@Daniel - top man! Thanks for all the help.
phrenetic
@Daniel oh, well said :) - great and complete answer :D
eglasius
+1  A: 

If you're doing the validation in the UserControl, you can have it offer a public ValidationFailed event and include the message in the EventArgs. The parent control could then subscribe to the ValidationFailed event and update the status bar.

Cory McCarty
Could you expand on that for me? I'm fairly new to c# and am not sure how you would do this. All help gratefully received.
phrenetic
@phrenetic see Daniel's update, its the same
eglasius
A: 

You can use asp.net validators in the user controls, and a validation summary on the main form and it will list the errors for you.

For other type of uses, you can expose an event, and have the page that contains the control subscribe to the event and take whatever action necessary.

eglasius