Search code examples
c#iterationcheckedlistbox

Odd problems with CheckListBoxes in C#


I have quite a few check boxes in an application I'm playing with. So, I decided to use a CheckedListBox instead. I iterate through the list with the code below...

private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckedListBox1.CheckedItems.Count != 0)
        {
            string x = "";
            for (int x = 0; x <= ServicesCheckedListBox3.CheckedItems.Count - 1; x++)
            {
                x = x + "Checked Item " + (x + 1).ToString() + " = " +                         ServicesCheckedListBox3.CheckedItems[x].ToString() + "\n";
            }
            Line.Add(x);
        }
    }

The out put gives me this...

System.Collections.Generic.List`1[System.String].

I'm very new and have never seen this. The application runs fine but the out put doesn't come out right. Any suggestions?


Solution

  • private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string x="";
        foreach(string chk in checkedListBox1.CheckedItems)
        {
            x = x + "Checked Item " + checkedListBox1.Items.IndexOf(chk).ToString() + " = " + chk + "\n";
        }
        MessageBox.Show(x);
    }