C#通过填写简单的注册表来显示错误消息 [英] C# Displaying error messages by completing a simple registration form

查看:55
本文介绍了C#通过填写简单的注册表来显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是陌生的,现在我学习C编程已有一年了.

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.

我的注册表格

当一个字段没有填写或没有很好地使用一个字段时,我想在按钮下方显示一条错误消息.

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

推荐答案

给出这样的类

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.

我还建议将您的 IsOkay 函数更改为

I suggest also to change your IsOkay function to

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

这还将处理仅由一个或多个空格组成的字符串.(不是null,不是length == 0)

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

这篇关于C#通过填写简单的注册表来显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆