Reputation: 47
I'm developing a Windows Forms app that has a lot of text and combo boxes which have to be entered manually. So there is a lot of checks if particular control is empty. I would like to get read of all validations in my UI and move them to the Middle Layer. This is great as UI is now validation free and exceptions are triggered as expected, but now I can't know which control causes exception to trigger. Well, I can, but not without intervention in my UI, which I obviously don't want, because this would make Middle layer validation unnecessary, as I could do it completely in UI. So, in short, what I would like to achieve is: if validation is triggered I would like to set focus to a control that causes exception, without focus hard setting in UI. Is this possible? Or if not, what would be the best solution? Any help appreciated.
I have created a simple example:
private void btnConfirm_Click(object sender, EventArgs e) { try { Customer.CustomerTN = txtCustomerTN.Text; Customer.CustomerName = txtCustomerName.Text; Customer.CustomerPhone = txtCustomerPhone.Text;MessageBox.Show("Customer TN: " + Customer.CustomerTN + Environment.NewLine + "Customer Name: " + Customer.CustomerName + Environment.NewLine + "Customer Phone: " + Customer.CustomerPhone); } catch (Exception ex) { MessageBox.Show(ex.Message); return; }
} //Middle Layer Class public class Customer { private static string customerTN; private static string customerName; private static string customerPhone;
public static string CustomerTN { get { return customerTN; } set { if (value.Length == 0) { throw new Exception("Enter Customer TN..."); } else { customerTN = value; } } } public static string CustomerName { get { return customerName; } set { if (value.Length == 0) { throw new Exception("Enter Customer Name..."); } else { customerName = value; } } } public static string CustomerPhone { get { return customerPhone; } set { if (value.Length == 0) { throw new Exception("Enter Customer Phone..."); } else { customerPhone = value; } } } }
Upvotes: 0
Views: 525
Reputation: 4219
You could create a hierarchy of Validation
classes. Every Validation
class would have a list
of controls to validate. When the validation takes place, if the control doesn't complies with the rules, you can abort validation by showing up a message and giving focus to that control, e.g.:
public abstract class ControlValidator<T> where T : Control
{
protected List<T> ControlsToValidate;
public ControlValidator(IEnumerable<T> controls)
{
this.ControlsToValidate = new List<T>(controls);
}
public abstract bool ValidateControls();
}
Then, if you want a validator for text boxes you would create a validator like:
public class TextBoxValidator : ControlValidator<TextBox>
{
public TextBoxValidator(IEnumerable<TextBox> controls) : base(controls)
{}
public override bool ValidateControls()
{
foreach(TextBox tb in ControlsToValidate)
{
if (tb.Text == "") // This validates the text cannot be empty
{
MessageBox.Show("Text cannot be empty");
tb.Focus();
return false;
}
}
return True;
}
}
Then you would create a list of validators to store all the validators of your app:
List<ControlValidator> validators = ...
To validate all your controls you would do a foreach like:
foreach(var validator in validators)
{
if (!validator.ValidateControls())
break;
}
The foreach is interrupted once it finds out that at least one control wasn't successfully validated. Hope it helps.
Upvotes: 1