1

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

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

3 回答 3

2

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

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

更新:

要存储每个文件的更改,请使用 a 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());
于 2013-08-28T04:24:09.917 回答
2

你可以使用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))));
于 2013-08-28T04:25:25.877 回答
1
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(....);
于 2013-08-28T05:06:19.763 回答