C# 在单个消息框中显示多条错误消息

C# 在单个消息框中显示多条错误消息,c#,validation,messagebox,C#,Validation,Messagebox,我目前正在开发一个带有产品维护页面的桌面应用程序,我正在寻找一种在单个消息框中显示所有验证错误的方法 我使用下面的代码为每个验证错误显示一个消息框:(验证绑定到保存按钮) if((Convert.ToInt32(txtQuantity.Text))>20000) { 显示(“最大数量为20000!”,“警告”,MessageBoxButtons.OK,MessageBoxIcon.Warning); txtQuantity.Focus(); 返回; } 如果((Convert.ToInt32(t

我目前正在开发一个带有产品维护页面的桌面应用程序,我正在寻找一种在单个消息框中显示所有验证错误的方法

我使用下面的代码为每个验证错误显示一个消息框:(验证绑定到保存按钮)

if((Convert.ToInt32(txtQuantity.Text))>20000)
{
显示(“最大数量为20000!”,“警告”,MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtQuantity.Focus();
返回;
}

如果((Convert.ToInt32(txtQuantity.Text))您可以使用StringBuilder并在其中添加错误:

StringBuilder sb = new StringBuilder();


 if ((Convert.ToInt32(txtQuantity.Text)) > 20000)
        {
              sb.AppendLine("Maximum quantity is 20,000!");            
        }



if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text)))
    {
       sb.AppendLine("Quantity is lower than Critical Level.");
    }

....
  MessageBox.Show(sb.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
StringBuilder sb=新建StringBuilder();
如果((Convert.ToInt32(txtQuantity.Text))>20000)
{
sb.AppendLine(“最大数量为20000!”);
}

如果((Convert.ToInt32(txtQuantity.Text))快速脏液将是:

    string errorMessages = String.Empty;

    if ((Convert.ToInt32(txtQuantity.Text)) > 20000)
    {
        errorMessages +="- Maximum quantity is 20,000!\r\n";
        txtQuantity.Focus();
        return;
    }

    if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text)))
    {
        errorMessages += "- Quantity is lower than Critical Level.\r\n";
        txtQuantity.Focus();
        return;
    }

    if (txtCriticalLevel.Text == "0")
    {
        errorMessages += "- Please check for zero values!\r\n";
        txtCriticalLevel.Focus();
        return;
    }

    if(!String.IsNullOrEmpty(errorMessages))
        MessageBox.Show(errorMessages, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
string errorMessages=string.Empty;
如果((Convert.ToInt32(txtQuantity.Text))>20000)
{
errorMessages+=“-最大数量为20000!\r\n”;
txtQuantity.Focus();
返回;
}

如果((Convert.ToInt32(txtQuantity.Text))谢谢你的回答!我会研究这个问题,但是我如何考虑验证?你能给我一个可能的例子吗?:)@RonReyes我更新我的答案;)当你的字符串比使用StringBuilder更好时,你的代码创造了奇迹!!!非常感谢!!但是你能解释一下为什么它“脏”吗?因为它需要一些重构来使用“StringBuilder”,而不是@M.Azad所说的字符串连接,并且可能需要一些字符串格式化。
    string errorMessages = String.Empty;

    if ((Convert.ToInt32(txtQuantity.Text)) > 20000)
    {
        errorMessages +="- Maximum quantity is 20,000!\r\n";
        txtQuantity.Focus();
        return;
    }

    if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text)))
    {
        errorMessages += "- Quantity is lower than Critical Level.\r\n";
        txtQuantity.Focus();
        return;
    }

    if (txtCriticalLevel.Text == "0")
    {
        errorMessages += "- Please check for zero values!\r\n";
        txtCriticalLevel.Focus();
        return;
    }

    if(!String.IsNullOrEmpty(errorMessages))
        MessageBox.Show(errorMessages, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);