AppleAssassin
AppleAssassin

Reputation: 493

Error message to show which text boxes are blank C#

Hey so I have all my code working and it's fine. But I'd like to clean it up a little.

At the moment I just have a message box show if there is an error in the input, so it would show "Please check your input", however I'd like it to show something like "Please check the following: firstname, secondname etc."

if  ((FirstnameText.Text.Trim().Length == 0) || (SurnameText.Text.Trim().Length == 0)
    || (DateOfBirthText.Text.Trim().Length == 0) || (CourseText.Text.Trim().Length == 0)
    || (MatricNoText.Text.Trim().Length == 0) || (YearMarkText.Text.Trim().Length == 0)
    || (int.Parse(MatricNoText.Text) < 10000 || int.Parse(MatricNoText.Text) > 99999)
    || (int.Parse(YearMarkText.Text) < 0 ||  int.Parse(YearMarkText.Text) > 100))

   {
      errorMessage();
      return;
   }

public void errorMessage()
{
   MessageBox.Show("Please check your input");
}

I know it's messy, but hey it works

Currently it just outputs that message, is there a simple way to output the specific textbox which has the error?

thanks

Upvotes: 0

Views: 3873

Answers (4)

andy
andy

Reputation: 6079

If possible you re-write your code like below

Control errorControl =null;
foreach (Control ctrl in this.Controls)
{
  if (ctrl is TextBox)
  {
    if (ctrl.Name == "MatricNoText")
    {
      if ((int.Parse(MatricNoText.Text) < 10000 || int.Parse(MatricNoText.Text) > 99999))
          {
            errorControl = ctrl;
          }
     }
     else if (ctrl.Name == "MatricNoText")
     {
       if (int.Parse(YearMarkText.Text) < 0 || int.Parse(YearMarkText.Text) > 100)
       {
         errorControl = ctrl;
        }
      }
      else
      {
        if (ctrl.Text.Length == 0)
        {
          errorControl = ctrl;
         }
       }
    }
  }

MessageBox.Show("Please check your input." + errorControl.Focus());

Upvotes: 0

L. Miller
L. Miller

Reputation: 347

The built-in ErrorProvider component will work wonders for your situation. Drag it from the toolbox onto the designer for your form. It will appear at the bottom, where NotificationIcons and ContextMenuStrips appear. The great thing about the ErrorProvider is it give a visual feedback icon with a mouse over tooltip next to the control.

You can then use the "Validating" event of the control to check what you need:

private void FirstnameText_Validating (object sender, CancelEventArgs e)
{
    string error = null;

    if (FirstnameText.Text.Length == 0)
    {
        error = "You must enter a First Name";
        e.Cancel = true; // This is important to keep focus in the box until the error is resolved.
    }

    ErrorProvider.SetError((Control)sender, error); // FirstnameText instead of sender to avoid unboxing the object if you care that much
}

You can also stick it in the Save button instead of raising it on the "Validating" event. To clean your code up even more, make a class that validates the input to keep non-UI stuff out of the UI.

Upvotes: 2

Matthias Meid
Matthias Meid

Reputation: 12513

I often use Fluent Validation. The WithMessage method lets you specify an error message. The validator then returns you an enumerable of all error messages. There might also be a better fitting method for your specific problem.

Upvotes: 0

lboshuizen
lboshuizen

Reputation: 2786

Splitting up your code would be a start:

if  ((FirstnameText.Text.Trim().Length == 0){
   errorMessage("firstname is empty");
}

if (SurnameText.Text.Trim().Length == 0){
   errorMessage("surname is empty");
}

Get the idea?

Upvotes: 1

Related Questions