Home > Mobile >  how to display contents of list in single messageBox
how to display contents of list in single messageBox

Time:05-11

I am comparing a list of strings with a reference list and storing the strings which are different from the reference list in another list. Now how can i display all the contents of new list using C# coding.

Please help me.

CodePudding user response:

The simplest option would be to just create a large string with one line per item in the list:

  var newLineSepratedString = string.Join(Environment.NewLine, myListOfStrings);

This can then be used as the message in your messageBox, or concatenated to some description.

Note that this works fine if the list is small. If you try to display to many items your messagebox will start to become larger than your screen, and that is just not useful. If you need to handle larger sections of text you should create your own dialog with a textbox that can be scrolled.

CodePudding user response:

You only have to build a string with all the list items. Then, you can put this new string into the MessageBox:

List<string> list = new List<string>()
{
    "Item 1",
    "Item 2",
    "Item 3"
};


string allItems = string.Empty;

foreach (string item in list)
{
    if (allItems.Length > 0)
    {
    allItems  = "\n";
    }
    else { } //TODO Nothing

    allItems  = item;
}

MessageBox.Show($"My Items:\n{allItems}", "Items", MessageBoxButtons.OK, MessageBoxIcon.Information);

This will print all items one down last one.

  • Related