C# Displaying error messages by completing a simple registration form

谁说胖子不能爱 提交于 2019-12-13 18:20:09

问题


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 ? :)


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/26663662/c-sharp-displaying-error-messages-by-completing-a-simple-registration-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!