C# Displaying error messages by completing a simpl

2019-08-05 06:38发布

I'm new about C#, I learnt C programmation for one year now.

I created a Window Form which asks the user to complete a registration form.

My registration form

I'd like to display an error message below the buttons when a field is not filled or a field isn't well used.

I used this basic code :

private void button1_Click(object sender, EventArgs e)
    {
        if (!isOkay(userTextBox.Text))
        {
            label5.Text = "Please, enter an username.";
            label5.Visible = true;
        }
        else if (!isOkay(mailTextBox.Text))
        {
            label5.Text = "Please, enter a mail address.";
            label5.Visible = true;
        }
        else if (!confirmMailTextBox.Text.Equals(mailTextBox.Text) || !isOkay(confirmMailTextBox.Text))
        {
            label5.Text = "Please, match both mails addresses.";
            label5.Visible = true;
        }
        else if (!isOkay(passwordTextBox.Text))
        {
            label5.Text = "Please, enter a password.";
            label5.Visible = true;
        }
        else
        {
            label5.Text = "Valid form, yay !";
            label5.Visible = true;
        }
    }

    private Boolean isOkay(string textBoxContent)
    {
        return (textBoxContent.Length > 0 || textBoxContent.Equals(null));
    }

Are there any elegant or optimized ways to do it properly ? I found some Error providers, but apparently error providers open a pop-up, and I just want a "red error message below buttons".

Can you give me some help ? :)

2条回答
手持菜刀,她持情操
2楼-- · 2019-08-05 06:52

Given a class like this

public class RequiredFieldsError
{
    private List<string> errors;
    public RequiredFieldsError()
    {
         errors =  new List<string>();
    }
    public int Count
    {
        get{return errors.Count;}
    }
    public void AddField(string errorField)
    {
        errors.Add(errorField);
    }
    public override string ToString()
    {
        if(errors.Count == 0)
           return string.Empty;
        else
        {
           string fields = string.Join(Environment.NewLine, errors);
           fields = "The following fields contains errors:" + Environment.NewLine + fields;
           return fields;
        }
    }
}

then you could change your code to

private void button1_Click(object sender, EventArgs e)
{
    RequiredFieldsError rfe = new RequiredFieldsError();

    if (!isOkay(userTextBox.Text))
       rfe.AddField("User name missing, Please, enter an username.";
    if (!isOkay(mailTextBox.Text))
       rfe.AddField("Email address missing, Please, enter a mail address.";
    if (!confirmMailTextBox.Text.Equals(mailTextBox.Text) || !isOkay(confirmMailTextBox.Text))
       rfe.AddField("Email address doesn't match the confirmation email");
    if (!isOkay(passwordTextBox.Text))
       rfe.AddField("Password missing, Please, enter a password.";

    if(rfe.Count > 0)
    {
        // MessageBox.Show(rfe.ToString());
        label5.Text = rfe.ToString()
        label5.Visible = true;
    }     
}

This approach avoids the unnerving situation (for your user) when he/she receives an error message, he/she fixes it just to receive another error message at the next attempt to confirm the form.

Of course your label should be tall enough to show all the possible messages or just use a messagebox.

I suggest also to change your IsOkay function to

private Boolean isOkay(string textBoxContent)
{
    return !string.IsNullOrWitheSpace(textBoxContent));
}

this will handle also a string composed just of one or more spaces. (not null and not length==0)

查看更多
Melony?
3楼-- · 2019-08-05 07:03

You can use ErrorProvider. It show's an error icon after your textbox.

For one of your textboxes for example:

     if (!isOkay(userTextBox.Text))
    {
        errorProvider1.SetError(userTextBox "yourmessage");
    }
    else{
        errorProvider1.Clear();
    }

Link: http://www.dotnetperls.com/errorprovider

查看更多
登录 后发表回答