c#中CheckListBoxes的奇怪问题

本文关键字:问题 CheckListBoxes | 更新日期: 2023-09-27 18:18:03

我正在使用的一个应用程序中有相当多的复选框。所以,我决定使用CheckedListBox代替。我用下面的代码遍历列表…

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);
        }
    }

输出给我这个…

System.Collections.Generic.List`1[System.String].
我是新来的,从来没见过这个。应用程序运行正常,但是输出不正确。有什么建议吗?

c#中CheckListBoxes的奇怪问题

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);
}

使用foreach遍历CheckedItems

foreach(string item in ServicesCheckedListBox3.CheckedItems)
{
    Line.Add(item)
}