WinForm:如何在ListBox中添加CheckBox

http://www.cnblogs.com/myshell/archive/2010/09/24/1834184.html

最近因为做WinForm的项目,遇到这个问题,当时以为CheckedListBox不能满足这个功能,所以采用了ListBox + CheckBox的组合。后来发现,CheckedListBox完全满足,但还是打算写在博客里,算是个总结。

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

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

原文地址:https://www.cnblogs.com/Echo529/p/6382119.html