5

直到现在我才处理过checkedListBox1。我想制作的程序将受益于使用它,而不是必须使用大量的复选框。

我有代码:

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int selected = checkedListBox1.SelectedIndex;
    this.Text = checkedListBox1.Items[selected].ToString();
}

这样做的问题是,每次我单击该框并突出显示时,它都会选择突出显示的对象。我正在寻找的是识别所选内容的变化,而不是突出显示。

我还想知道的是,如果 CheckListBox 中的第一个索引项以及第三个被选中,我将如何检查它是否正确?

我敢肯定我最终会弄明白的,但是看到代码会有很大帮助。

假设我有 3 个盒子: Box A = messageBox.Show("a"); 框 B = messageBox.Show("b"); 框 C = messageBox.Show("c");

如果选中该框,它将仅显示 mbox。我想知道的是如何让它检查例如是否检查了 A 和 C,以便如果我按下按钮,两个消息框将显示“a”然后显示“c”

4

2 回答 2

7
   private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        // a checkbox is changing
        // but value is not updated yet

    }

    private void checkedListBox1_MouseUp(object sender, MouseEventArgs e)
    {
        Debug.WriteLine(checkedListBox1.CheckedItems.Count);
        Debug.WriteLine(checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[0]));
    }

我认为您应该在 MouseUp 中检查是否检查了第一个。_ItemCheck 用于复选框正在更改但值尚未更新时。

请参阅参考:http: //msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.items.aspx

   // First show the index and check state of all selected items. 
foreach(int indexChecked in checkedListBox1.CheckedIndices) {
    // The indexChecked variable contains the index of the item.
    MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                    checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
}

// Next show the object title and check state for each item selected. 
foreach(object itemChecked in checkedListBox1.CheckedItems) {

    // Use the IndexOf method to get the index of an item.
    MessageBox.Show("Item with title: \"" + itemChecked.ToString() + 
                    "\", is checked. Checked state is: " + 
                    checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
}
于 2012-10-25T14:54:11.727 回答
1

如果要获取所有选中项的集合,请使用 checkedListBox1.CheckedItems。要在单击按钮时显示所有选中的项目,请使用以下命令:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
        MessageBox.Show(checkedListBox1.CheckedItems[i].ToString());
}

如果您只需要它们的索引,请改用checkedListBox1.CheckedIndices。

于 2015-01-25T02:01:52.947 回答