【问题标题】:Programmatically selecting items in winforms checkboxlist以编程方式选择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;
            }
        }

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

【问题讨论】:

    标签: winforms checkboxlist


    【解决方案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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-02
        • 2017-12-11
        • 2012-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-08
        相关资源
        最近更新 更多