private void button1_Click(object sender, EventArgs e)
{
string[] AnalyteNames = new string[]{
"NA", "K", "CL", "TCO2", "BUN", "CREA", "EGFR", "GLU", "CA", "ANG", "HCT", "HGB" };
for (int i = 0; i < AnalyteNames.Length; i++)
{
CheckBox box = new CheckBox();
box.Text = AnalyteNames[i].ToString();
box.ForeColor = Color.Black;
box.Checked = false;
// AnalyteListCheckBoxes was a CheckedBoxList
this.AnalyteListCheckBoxes.Items.Add(box);
}
}
答案 0 :(得分:0)
您所要做的就是放置AddRange
而不是循环:
private void button1_Click(object sender, EventArgs e) {
string[] AnalyteNames = new string[]{
"NA", "K", "CL", "TCO2", "BUN", "CREA", "EGFR", "GLU", "CA", "ANG", "HCT", "HGB" };
// Create checkboxes with provided titles
// checkboxes will be unchecked by default
AnalyteListCheckBoxes.Items.AddRange(AnalyteNames);
}
请注意,您不应添加CheckBox
个项目,String
,因为CheckedListItems
会在内部创建项目。
答案 1 :(得分:0)
您可以使用此overload of Add:
this.AnalyteListCheckBoxes.Items.Add(box.Text, box.Checked);
另一种解决方案可能是设置DisplayMember
属性,以便编译器知道它应该显示什么。然后你可以保留你的代码,并在循环后包含这一行:
checkedListBox1.DisplayMember = "Text";