创建一个返回结果数组以显示在messagebox中

本文关键字:数组 显示 messagebox 结果 返回 一个 创建 | 更新日期: 2023-09-27 18:29:12

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

 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");
                }
            }

创建一个返回结果数组以显示在messagebox中

使用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());

您可以使用StringBuilder,如下所示

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

或者你可以使用LINQ

MessageBox.Show(string.Join("'n" ,Comparer.GroupBy(x => x.Value > 0)
                    .Select(g => string.Join("'n" , 
                            g.Select(l=> "Extra value 'n" + l.Key + (Math.Abs(l.Value) != 1 ? ("'n" + Math.Abs(l.Value) + "times") : string.Empty))) +
                                " 'nfound in file " + (g.Key ? firstFile : secondFile))));
foreach (KeyValuePair<string, int> kvp in Comparer)
        {
            if (kvp.Value != 0)
            {
                mismatches++;
                string InWhich = kvp.Value > 0 ? firstFile : secondFile;
                //load the data in to a string - use string append
            }
        }
//outside the foreach loop
MessageBox.Show(....);