In input validation I'm using this validation method in my all Forms. Here I have a single handler for all TextBoxes
in my Form and if I have other controllers like ComboBoxes
I would use a separate handler
for all ComboBoxes
and so on.
My only doubt in this approach is how users would feel about this as you can't focus on other text boxes while keeping the current one empty if it's not an optional field!
Could you please review this and provide your feedback?
public partial class frmForm : Form
{
public frmForm()
{
InitializeComponent();
foreach (TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.CausesValidation == true))
{
tb.Validating += textBox_Validating;
}
}
private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox currenttb = (TextBox)sender;
if (currenttb.Name != OptionalFields .txtPolicy.ToString())
{
if (string.IsNullOrWhiteSpace(currenttb.Text))
{
MessageBox.Show(string.Format("Empty field {0 }", currenttb.Name.Substring(3)));
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
}
private enum OptionalFields
{
txtPolicy, txtRemarks
}
}
*
beside the required fields. This a common practice for input forms. This way the user knows what is expected. – tinstaafl Jun 19 at 13:22