user2101459
user2101459

Reputation: 599

Display items in a listbox in a messagebox C#

I am writing a windows form application that requires me to print the items of a listbox in a messagebox and well . . . here is what I have:

private void btnDisplay_Click(object sender, EventArgs e)
{
    StringBuilder str = new StringBuilder();
    foreach (object selectedItem in ListBoxCondiments.Items)
    {
        str.AppendLine(ListBoxCondiments.Items.ToString());
    }
    MessageBox.Show("Your made-to-order Burger will include:" + str, "Custom Burger!");
}

And as a result I am receiving a messagebox with string and instead of the items in my list I receive System.Windows.Forms.CheckedListBox + . . . (until the end of the list)

thank you for your help!

Upvotes: 1

Views: 5588

Answers (2)

Doan Cuong
Doan Cuong

Reputation: 2614

Change this

str.AppendLine(ListBoxCondiments.Items.ToString())

Into this

str.AppendLine(selectedItem.ToString())

Upvotes: 0

mservidio
mservidio

Reputation: 13057

You would want to use selectedItem.ToString().

str.AppendLine(selectedItem.ToString());

Upvotes: 2

Related Questions