简体   繁体   中英

Displaying selected items of listbox into a message box in C# Windows Form

I am able to display multiple selected items from a listbox into a text box on a button click but how can I display the same on a message box? I mean displaying first item on a messagebox is not an issue but multiple items at once is. Suggestions please...

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{

    Skill = checkedListBox1.SelectedItem.ToString();
    if (e.NewValue == CheckState.Checked)
    {
        listBox1.Items.Add(Skill);
    }
    else
    {
        listBox1.Items.Remove(Skill);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("You have selected following Skills : \n"+Skill, "Selected Skills",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
}

You do not show where Skill is defined, but presumably as a property of the class. You only initialize Skill in checkedListBox1_ItemCheck . If the selection has changed, that value will be stale (it will not reflect reality).

The shortest change to your code would be to not use Skill in your button handler, but rather grab the current state from the listbox (possibly put it in a local variable if you prefer that style).

private void button1_Click(object sender, EventArgs e)
{
    var selectedSkills = checkedListBox1.SelectedItem.ToString();
    MessageBox.Show("You have selected following Skills : \n"+selectedSkills, "Selected Skills",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
}

It looks like you are overwriting Skill with the last value to be checked. So, I would expect the message box to always show the Skill related to the last item that you clicked on. So, if you want to display all of them, you'll need to replace Skill in the MessageBox.Show call with something like listBox1.Items.Cast<string>().Aggregate((o, n) => o.ToString() + "," + n.ToString())

*Note: replace Cast<string> with whatever type of object Skill is.

Such as:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    Skill = checkedListBox1.SelectedItem.ToString();

    if (e.NewValue == CheckState.Checked)
    {
        listBox1.Items.Add(Skill);
    }
    else
    {
        listBox1.Items.Remove(Skill);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("You have selected following Skills : \n"+ listBox1.Items.Cast<string>().Aggregate((o, n) => o.ToString() + "," + n.ToString()), "Selected Skills",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
}

You must be looping through the selected items and appending them to your textbox. In order to show on message box you need to concatenate the selected items to a string variable and use it for your message.

private void button1_Click(object sender, EventArgs e)
{
    StringBuilder skills = new StringBuilder();
    foreach (object item in listBox1.SelectedItems)
    {
        skills.AppendLine(item.ToString());
    }

    MessageBox.Show("You have selected following Skills : \n" + skills.ToString(), "Selected Skills",
    MessageBoxButtons.OK, MessageBoxIcon.Information);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM