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

时间:2013-08-28 04:18:03

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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))));

答案 2 :(得分: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(....);