以编程方式选择winforms checkboxlist中的项目

Posted

技术标签:

【中文标题】以编程方式选择winforms checkboxlist中的项目【英文标题】:Programmatically selecting items in winforms checkboxlist 【发布时间】:2011-04-30 11:15:19 【问题描述】:

我不知道如何以编程方式选择复选框列表中的项目。

这个方法无法编译,但我想告诉你我想要得到什么结果。

public ColumnsSelector(Dictionary<string, bool> dataPropertyNames)
            : this()
        
            foreach (var item in dataPropertyNames)
            
                checkedListBox1.Items.Add(item.Key);
                checkedListBox1.Items[checkedListBox1.Items.IndexOf(item.Key)].Checked = item.Value;
            
        

你是如何解决这个问题的?

【问题讨论】:

【参考方案1】:

使用CheckedListBox.SetItemCheckState:

checkedListBox.SetItemCheckState(checkedListBox1.Items.Count - 1, CheckedState.Checked);

适用于已检查、未检查和不确定。你也可以使用CheckedListBox.SetItemChecked:

checkedListBox.SetItemChecked(checkedListBox1.Items.Count - 1, true);

【讨论】:

【参考方案2】:
 checkedListBox1.Items.Add(item.Key);
 checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, item.Value);

或者只是

 checkedListBox1.Items.Add(item.Key, item.Value);

【讨论】:

以上是关于以编程方式选择winforms checkboxlist中的项目的主要内容,如果未能解决你的问题,请参考以下文章