c# - 创建一个返回结果数组以显示在消息框中

标签 c# winforms

我有这个方法可以向用户返回一个显示结果的消息框。但是对于多个结果,每个结果都会出现消息窗口。我如何创建它以便将结果放入一个数组中,然后在最后在一个消息框中向用户显示所有结果?

 foreach (KeyValuePair<string, int> kvp in Comparer)
            {
                if (kvp.Value != 0)
                {
                    mismatches++;
                    string InWhich = kvp.Value > 0 ? firstFile : secondFile;
                    MessageBox.Show("Extra value \n"+kvp.Key+" \nfound in file " + InWhich);
                    if (Math.Abs(kvp.Value) != 1)
                        MessageBox.Show( Math.Abs(kvp.Value)+ "times");
                }
            }

最佳答案

使用 StringBuilder在循环中构建字符串,如下所示:

System.Text.StringBuilder theStringBuilder = new System.Text.StringBuilder();
foreach (KeyValuePair<string, int> kvp in Comparer)
{
    if (kvp.Value != 0)
    {
        mismatches++;
        string InWhich = kvp.Value > 0 ? firstFile : secondFile;
        theStringBuilder.AppendLine("Extra value \n"+kvp.Key+" \nfound in file " + InWhich);
        if (Math.Abs(kvp.Value) != 1)
            theStringBuilder.AppendLine( Math.Abs(kvp.Value)+ "times");
        }
    }
}

现在你可以显示StringBuilder中的所有内容了,像这样:

// Display all results in message box
MessageBox.Show(theStringBuilder.ToString());

更新:

要存储每个文件的更改,请使用 List<string> ,像这样:

var firstFileChanges = new List<string>();
var secondFileChanges = new List<string>();

System.Text.StringBuilder theStringBuilder = new System.Text.StringBuilder();
foreach (KeyValuePair<string, int> kvp in Comparer)
{
    if (kvp.Value != 0)
    {
        mismatches++;
        string InWhich = kvp.Value > 0 ? firstFile : secondFile;

        if(InWhich == firstFile)
        {
            firstFileChanges.Add(kvp.Key);
        }
        else 
        {
            secondFileChanges.Add(kvp.Key);
        }
    }
}

if(firstFileChanges.Count > 0 )
{
    theStringBuilder.Append("First file changes: ");

    int counter1 = 0;
    foreach(string change1 in firstFileChanges)
    {
        if(counter1 > 0)
        {
            theStringBuilder.Append(", ");
        }

        theStringBuilder.Append(change1);

        counter1 += 1;
    }

    theStringBuilder.AppendLine();
}

if(secondFileChanges.Count > 0 )
{
    theStringBuilder.Append("Second file changes: ");

    int counter2 = 0;
    foreach(string change2 in secondFileChanges)
    {
        if(counter2 > 0)
        {
            theStringBuilder.Append(", ");
        }

        theStringBuilder.Append(change2);

        counter2 += 1;
    }
}

MessageBox.Show(theStringBuilder.ToString());

关于c# - 创建一个返回结果数组以显示在消息框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18479374/

相关文章:

c# - 在 PictureBox() 中绘制矩形或箭头

c# - 下载动态生成的图像而不是在浏览器中显示

c# - 语音命令,使用 .NET 的语音识别

c# - 如何禁用 ModelMetadata.IsRequired 对于不可空值类型始终为真

c# - WinForms:列表框项目更新不区分大小写?

c# - 尝试循环访问 Form 上的 Button 控件时出现 'Unable to cast object of type' 错误

c# - 我可以更改内插字符串表达式的颜色吗?

c# - UserControl 中的 DependencyProperty bool

c# - winform控件中文本中的一个粗体字

c# - 按 Enter 键单击一个 LinkLabel