Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>To handle validation do one of these:</strong></p> <ul> <li>Validate with a method inside the user control</li> <li><p>Have your user control have a <a href="http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx" rel="nofollow noreferrer"><code>delegate</code></a> property (e.g. <code>ValidationHandler</code>) 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)</p> <p><code>public delegate void Validator(...)</code></p> <p><code>public Validator ValidationHandler { get; set; }</code></p></li> <li><p>Have your user control generate a validation request <a href="http://msdn.microsoft.com/en-us/library/awbftdfh.aspx" rel="nofollow noreferrer"><code>event</code></a> (e.g. <code>ValidationRequested</code>)</p> <p><code>public event EventHandler&lt;ValidationEventArgs&gt; ValidationRequested</code></p></li> </ul> <p><strong>To notify the system that an error has occurred do one of these:</strong></p> <ul> <li><p>Use an <a href="http://msdn.microsoft.com/en-us/library/awbftdfh.aspx" rel="nofollow noreferrer"><code>event</code></a> that interested parties can subscribe to (e.g. <code>ValidationFailed</code>)</p></li> <li><p>If the object that performs the validation (via the <code>delegate</code> or <code>event</code>) is also the one that you want to generate the error message from, it can raise the error message itself.</p></li> </ul> <p><strong>EDIT:</strong></p> <p>Since you've said you would validate inside your control, the code for a ValidationFailed event might look like:</p> <pre><code>// In your user control public class ValidationFailedEventArgs : EventArgs { public ValidationFailedEventArgs(string message) { this.Message = message; } public string Message { get; set; } } private EventHandler&lt;ValidationFailedEventArgs&gt; _validationFailed; public event EventHandler&lt;ValidationFailedEventArgs&gt; 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&lt;ValidationFailedEventArgs&gt;(userControl_ValidationFailed); // ... private void userControl_ValidationFailed(object sender, ValidationFailedEventArgs e) { statusBar.Text = e.Message; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload