如何在ListBox中添加CheckBox

2025-01-31 06:28:41
推荐回答(1个)
回答1:

实现其实很简单,只是我们在通过ListBox的Controls属性添加CheckBox时,要设置CheckBox的Location值,不然,添加多个CheckBox会只显示一个。如下代码所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
代码

string[] list = new string[] { "张三", "李四", "王五" };

int x = 0, y = 0;
foreach (string item in list)
{
CheckBox cb = new CheckBox();
cb.Text = item;
cb.Location = new Point(x, y);
clbInvisibleColumn.Controls.Add(cb);
y += 22;
}