Like the title says, I am trying to display the items a user selects from a ListBox in a MessageBox. This MessageBox is supposed to be part of a menu strip. I was wondering if anyone might be able to help me with this. Here is the related code:
private void displaySelectionToolStripMenuItem_Click(object sender, EventArgs e)
{
// this is where the message box is supposed to be
}
if (checkedListBox1.SelectedItem.ToString() == "Fudge")
{
cost +=6;
listBox1.Items.Add("Fudge");
textBox1.Text = cost.ToString("C");
}
if (checkedListBox1.SelectedItem.ToString() == "Caramel")
{
cost +=4;
listBox1.Items.Add("Caramel");
textBox1.Text = cost.ToString("C");
}
Unsuccessful Attempts of Coding this
private void displaySelectionToolStripMenuItem_Click(object
sender, EventArgs e)
{
string display = (listBox1.SelectedItems.ToString());
for (int i = 0; i < display.Length; i++
{
MessageBox.Show(display)
}
}
private void displaySelectionToolStripMenuItem_Click(object
sender, EventArgs e)
{
MessageBox.Show(listBox1.SelectedItems.ToString());
}
private void displaySelectionToolStripMenuItem_Click(object
sender, EventArgs e)
{
List <string> display = new List <string>();
string str = null;
foreach (var item in listBox1.SelectedItems)
{
display.Add(item.ToString());
str += item + "\r\n";
}
MessageBox.Show(str);
}
I have a much greater number of methods dealing with the various options the user can choose, but they appear repetitive enough that most of them can be omitted. Please let me know if these methods are insufficient for my question.
So if both Fudge and Caramel are selected, I would like both of these items to appear in a message box when "Display Selection" on the ToolStripMenu is selected.
I attempted a wide variety of solutions over the past few hours, but none of them were helpful. I am repeatedly getting "System.Windows.Forms.ListBox" as the output in the message box, as opposed to the selected items.