GJJ
GJJ

Reputation: 494

Displaying elements inside an array list with a messagebox

I have an array list called str, i want to see the elements inside that array list, how should I use a messagebox to display that?

p.s. displaying the first field of the element is also fine

example code(a method I created to retrieve data from api:

void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result;
                StreamReader responseReader = new StreamReader(responseStream);
                string response = responseReader.ReadToEnd();

                string[] split1 = Regex.Split(response, "},{");
                List<string> pri1 = new List<string>(split1);
                pri1.RemoveAt(0);
                string last = pri1[pri1.Count() - 1];
                pri1.Remove(last);

                str = pri1;
            }
        }

Upvotes: 2

Views: 11130

Answers (4)

Alex Aza
Alex Aza

Reputation: 78467

To show comma delimited values:

var list = new List<string> { "tes1", "test2", "test3" };

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

[Update]

A few enhancements you could make in your code:

private void button1_Click(object sender, RoutedEventArgs e)
{
    var response = "asdf},{asaaa},{shf";
    var split = Regex.Split(response, "},{");

    var elements = split
        .Skip(1)
        .Take(split.Length - 2)
        .ToList();

    var message = string.Join(",", elements);
    MessageBox.Show(message);
}

Upvotes: 2

Melodatron
Melodatron

Reputation: 610

I usually do something like this:

    public void PrintList(IList<object> list)
    {
        string printString = "List Elements";

        foreach (object o in list)
        {
            // Add the fields you want to show here
            printString += "\n" + o.ToString();
        }

        MessageBox.Show(printString);
    }

For your implementation, replace object with your class that stores the information you've retreived from the API and add the fields you want to see to the printString += ... line.

Upvotes: 1

abhilash
abhilash

Reputation: 5651

Linq adds flexibility to the desired format:

var list = new List<string>{"one","two", "three"};

var consolidatedList = list.Aggregate((s1,s2) => String.Format("{0} {1}",s1,s2));
//consolidatedList = "one two three"

MessageBox.Show(consolidatedList)

Upvotes: 0

Oliver Weichhold
Oliver Weichhold

Reputation: 10296

MessageBox.Show(String.Join(" ", str) but are you really sure that you want this? I mean wouldn't it be more attractive to present that list inside an ItemsControl shown inside a Silverlight ChildWindow?

<ChildWindow>
 <ItemsControl ItemsSource="{Binding str}">
   <ItemsControl.ItemTemplate>
     <DataTemplate>
       <TextBlock Text="{Binding}"/>
     </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl
</ChildWindow>

Upvotes: 1

Related Questions