显示MessageBox show方法中的错误列表

Jar*_*tte 2 c#

我正试图找出一种方法,使用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;
    }
Run Code Online (Sandbox Code Playgroud)

但是,你不能在Show方法中使用String类型的List.有任何想法吗?

jda*_*ies 6

您可以使用以下内容:

        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);
Run Code Online (Sandbox Code Playgroud)