public void Validate() { // Validate all validators on this form, ensuring first invalid // control (in tab order) is selected Control firstInTabOrder = null; ValidatorCollection validators = ValidatorManager.GetValidators(_hostingForm); if (null == validators) { return; } foreach (BaseValidator validator in validators) { // Validate control validator.Validate(); // Record tab order if before current recorded tab order if (!validator.IsValid) { if ((firstInTabOrder == null) || (firstInTabOrder.TabIndex > validator.ControlToValidate.TabIndex)) { firstInTabOrder = validator.ControlToValidate; } } } // Select first invalid control in tab order, if any if (firstInTabOrder != null) { firstInTabOrder.Focus(); if (firstInTabOrder is TextBoxBase) { TextBoxBase textbox = firstInTabOrder as TextBoxBase; textbox.SelectionStart = 0; textbox.SelectionLength = textbox.Text.Length; } } }
private void Form_Closed(object sender, EventArgs e) { // DeRegister from ValidatorCollection ValidatorManager.DeRegister(this, (Form)sender); }
private void Form_Load(object sender, EventArgs e) { // Register with ValidatorManager ValidatorManager.Register(this, (Form)sender); }