Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two kinds of validation: data-specific validation (in the persistence layer) and user-interface validation. I prefer to put validation near the input side, because generally you want to show the user what's wrong, and trying to connect data validation to the user interface adds more indirection that has to match the data binding indirection.</p> <p>Putting data validation in control classes does not seem like a good idea. That basically means the control class can be used only for one specific field.</p> <p>The standard Windows Forms way of doing things is to put data validation in <i>container</i>. That way the validation can check against the state of other properties and connect the specific control to the ErrorProvider object(s) to display a pertinent error message.</p> <blockquote><code><pre>class EmployeeForm : UserControl { EmployeeObject employee; // ... void employeeNameTextBox_Validating (object sender, CancelEventArgs e) { if ( employee.Name.Trim ().Length == 0 ) { errorProvider.SetError (employeeNameTextBox, "Employee must have a name"); e.Cancel = true; } } void employeeHireDateControl_Validating (...) { if ( employee.HireDate &lt; employee.BirthDate ) { errorProvider.SetError (employeeHireDateControl, "Employee hire date must be after birth date"); e.Cancel = true; } } } class ExplorerStyleInterface : ... { // ... bool TryDisplayNewForm (Form oldForm, Form newForm) { if ( ! oldForm.ValidateChildren () ) return false; else { HideForm (oldForm); ShowForm (newForm); return true; } } }</pre></code></blockquote> <p>The standard WF way is to fire the Validating event for the specific control when the control loses focus or when ValidateChildren is called on the container (or the container's container). You set up a handler for this event through the event properties for the control on the container; the handler is automatically added to the container.</p> <p>I'm not sure why this way isn't working for you, unless you don't like the default behavior of refusing to reliquish focus on error, which you can change by setting the AutoValidate property of the container (or the container's container) to EnableAllowFocusChange.</p> <p>Tell us specifically what you don't like about the standard Windows Forms way of doing things, and maybe we can either offer alternatives or persuade you the standard way will do what you want. </p>
 

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