列表视图中不显示动态添加的复选框

dynamically added check boxes are not displaying in listview

我正在尝试在列表视图框中动态插入复选框。添加时滚动条会滚动,但我在列表视图框中看不到复选框。就好像他们是隐形的。

另一件事是当我将代码从 listBox1.Items.Add(box) 更改为 listBox1.Controls.Add(box) 时,我在列表框中只看到一个复选框。

这是我的代码,如下所示:

vouchersList 是一个包含 15 个字符串的列表。

 for (int i = 0; i < vouchersList.Count; i++)
 {
     CheckBox box = new CheckBox();
     box.Tag = i.ToString();
     box.Text = vouchersList[i];     
     listBox1.Items.Add(box);

 }  

你可以使用 CheckedListBox 代替 ListBox,然后你可以做这样的事情:

        CheckedListBox ClistBox1 = new CheckedListBox();
        ClistBox1.FormattingEnabled = true;
        ClistBox1.Location = new System.Drawing.Point(12, 12);
        ClistBox1.Name = "listBox1";
        ClistBox1.Size = new System.Drawing.Size(278, 290);
        ClistBox1.TabIndex = 0;
        this.Controls.Add(ClistBox1);

        for (int i = 0; i < 20; i++)
        {
            ClistBox1.Items.Add("Box" + i, true); //Second parameter is "Checked" true or false
        }