1

I'm trying to do a validation wherein if a checkbox has been ticked, the corresponding textbox would no longer be in ReadOnly mode and should not be empty. For example, if I checked CheckBox1, if TextBox1 did not have any input, a MessageBox would pop up to say that "Please fill up the entire form!". Else, it would display "Done!".

This is what I have so far:

if ((CheckBox1.Checked && TextBox1.Text == "") 
    || (CheckBox2.Checked && TextBox2.Text == ""))
    MessageBox.Show("Please fill up the entire form!");

else if (CheckBox1.Checked && TextBox1.Text != "")
    MessageBox.Show("Done!");

else if (CheckBox2.Checked && TextBox2.Text != "")
    MessageBox.Show("Done!");

I've made a couple of checkboxes/textboxes that would require this validation and I find that it gets kind of lengthy so I was wondering if there's a simpler/better approach.

(not sure if relevant) Note: I got the toggling the ReadOnly mode when the CheckChanged event is triggered part down

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
nik
  • 65
  • 6

1 Answers1

1

There could be some enhancements for your code, for example:

  • You can use this criteria !textBox.ReadOnly && string.IsNullOrEmpty(textBox.Text) rather than what you have.
  • You can avoid using those else if parts and just return from the method if there is a validation error and just put the code after the validation block.

A better solution - Using Validating Event

But I'd rather to change the whole style of validation and use Validating event of those TextBox controls. To do so, you need to follow these instructions:

1) Set AutoValidate property of form to EnableAllowFocusChange in design mode or using code in Load event of form.

2) Handle Validating event of all TextBox controls using single a method and set e.Cancel = true; when there is validation error:

private void textBox_Validating(object sender, CancelEventArgs e)
{
    var textBox = (TextBox)sender;
    if (!textBox.ReadOnly && string.IsNullOrEmpty(textBox.Text))
        e.Cancel = true;
}

3) In the save button of your form, using ValidateChildren method of the form, check if there is any validation error, show a message, otherwise do what the button is supposed to do:

private void button1_Click(object sender, EventArgs e)
{
    if (!this.ValidateChildren())
        MessageBox.Show("Please correct validation errors.")
    else
        MessageBox.Show("Done!")
}

Note

To read more about validation options in Windows Forms, take a look at this post:

Also if you want to enhance the user experience by showing a validation error/icon near the control using an ErrorProvider, take a look at this post:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Oh so this is what you meant. Thank you so much for that elaborate explanation. Worked like a charm! :) – nik Oct 16 '17 at 03:25
  • 1
    I recommend you to take a look at linked posts as well. Maybe it seems too much for a simple application, but knowing facilities is a key point which will help you when creating more complicated and more professional applications. – Reza Aghaei Oct 16 '17 at 03:36
  • I got around to just now. The detailed explanations are really helpful. Thank you! – nik Oct 16 '17 at 13:53