简体   繁体   中英

dynamically added check boxes are not displaying in listview

I am trying to insert check boxes in list view box dynamically. The scroll bar scrolls when they are added but I can't see check boxes in list view box. it's like they are invisible.

and the other thing is when I change the code from listBox1.Items.Add(box) to listBox1.Controls.Add(box) I see only one checkbox in list box.

This is my code as shown below :

vouchersList is a list of 15 strings.

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

 }  

you can use CheckedListBox insted of ListBox, and then you make something like this:

        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
        }  

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