C# 在MessageBox show方法中显示错误列表

C# 在MessageBox show方法中显示错误列表,c#,C#,我正试图找到一种方法,使用MessageBox.show在我的应用程序中显示验证错误列表。到目前为止,我有: private bool FormIsValid() { bool isValid = true; List<string> strErrors = new List<string>(); if (!(txtFirstName.Text.Length > 1) || !(txtLastName

我正试图找到一种方法,使用MessageBox.show在我的应用程序中显示验证错误列表。到目前为止,我有:

    private bool FormIsValid()
    {
        bool isValid = true;
        List<string> strErrors = new List<string>();

        if (!(txtFirstName.Text.Length > 1) || !(txtLastName.Text.Length > 1))
        {
            strErrors.Add("You must enter a first and last name.");
            isValid = false;
        }
        if (!txtEmail.Text.Contains("@") || !txtEmail.Text.Contains(".") || !(txtEmail.Text.Length > 5))
        {
            strErrors.Add("You must enter a valid email address.");
            isValid = false;
        }
        if (!(txtUsername.Text.Length > 7) || !(pbPassword.Password.Length > 7) || !ContainsNumberAndLetter(txtUsername.Text) || !ContainsNumberAndLetter(pbPassword.Password))
        {
            strErrors.Add("Your username and password most both contain at least 8 characters and contain at least 1 letter and 1 number.");
            isValid = false;
        }

        if (isValid == false)
        {
            MessageBox.Show(strErrors);
        }

        return isValid;
    }
private bool FormIsValid()
{
bool isValid=true;
List strErrors=新列表();
如果(!(txtLastName.Text.Length>1)| |!(txtLastName.Text.Length>1))
{
添加(“必须输入名字和姓氏”);
isValid=false;
}
如果(!txtmail.Text.Contains(“@”)| |!txtmail.Text.Contains(“.”| |!(txtmail.Text.Length>5))
{
添加(“您必须输入有效的电子邮件地址”);
isValid=false;
}
如果(!(txtUsername.Text.Length>7)| |!(pbPassword.Password.Length>7)| |!ContainsNumberAndLetter(txtUsername.Text)| |!ContainsNumberAndLetter(pbPassword.Password))
{
添加(“您的用户名和密码都至少包含8个字符,并且至少包含1个字母和1个数字。”);
isValid=false;
}
if(isValid==false)
{
MessageBox.Show(strErrors);
}
返回有效;
}
但是遗憾的是,您不能在Show方法中使用String类型的列表。有什么想法吗?

为什么不使用字符串

private bool FormIsValid()
{
    bool isValid = true;
    string strErrors = string.Empty;

    if (!(txtFirstName.Text.Length > 1) || !(txtLastName.Text.Length > 1))
    {
        strErrors = "You must enter a first and last name.";
        isValid = false;
    }
    if (!txtEmail.Text.Contains("@") || !txtEmail.Text.Contains(".") || !(txtEmail.Text.Length > 5))
    {
        strErrors += "\nYou must enter a valid email address.";
        isValid = false;
    }
    if (!(txtUsername.Text.Length > 7) || !(pbPassword.Password.Length > 7) || !ContainsNumberAndLetter(txtUsername.Text) || !ContainsNumberAndLetter(pbPassword.Password))
    {
        strErrors += "\nYour username and password most both contain at least 8 characters and contain at least 1 letter and 1 number.";
        isValid = false;
    }

    if (isValid == false)
    {
        MessageBox.Show(strErrors);
    }

    return isValid;
}

您可以使用以下选项:

        List<string> errors = new List<string>();
        errors.Add("Error 1");
        errors.Add("Error 2");
        errors.Add("Error 3");

        string errorMessage = string.Join("\n", errors.ToArray());
        MessageBox.Show(errorMessage);
List errors=newlist();
错误。添加(“错误1”);
错误。添加(“错误2”);
错误。添加(“错误3”);
string errorMessage=string.Join(“\n”,errors.ToArray());
MessageBox.Show(errorMessage);

StringBuilder通常会表现得更好,但对于这里这样少的字符串来说,它并没有那么重要。