Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put the contents of a list in a single MessageBox?

Basically, I have a list with multiple items in it and I want a single message box to display them all.

The closest I have got is a message box for each item (using foreach).

What I want is the equivalent of:

MessageBox.Show ("List contains:"+

Foreach (string str in list) 
{ str + Environment.Newline + }

                )

But obviously this won't work! What is the correct way of doing this?

like image 405
Wil Avatar asked Mar 02 '11 03:03

Wil


1 Answers

You can join everything into a single string with string.Join:

var message = string.Join(Environment.NewLine, list);
MessageBox.Show(message);

However, if you don't have access to .NET 4, you don't have that overload that takes an IEnumerable<string>. You will have to fallback on the one that takes an array:

var message = string.Join(Environment.NewLine, list.ToArray());
MessageBox.Show(message);
like image 159
R. Martinho Fernandes Avatar answered Oct 12 '22 03:10

R. Martinho Fernandes