Recognize CheckedListBox item has been selected

吃可爱长大的小学妹 提交于 2019-12-10 14:19:40

问题


I've never dealt with checkedListBox1 until now. A program that I want to make will benefit from using it rather than having to use numerous Checkboxes.

I have the code:

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

The problem with this is that each time I click on the box and it highlights, it then selects the highlighted object. What I am looking for is it to recognize a change in what has been selected, not highlighted.

What I'm also wanting to know is say if the first index item in CheckListBox is checked as well as the 3rd, how would I check to see if it is true or not?

I'm certain I will eventually figure it out but seeing the code would immensely help.

Say I have 3 boxes: Box A = messageBox.Show("a"); Box B = messageBox.Show("b"); Box C = messageBox.Show("c");

It will only display the mbox if the box is checked. What I want to know is how can I have it check to see if, for example, A and C is checked so that if I pressed a button, the two messageBoxes will display either "a" and then "c"


回答1:


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

I think you should check it in MouseUp for whether the 1st is checked. and _ItemCheck is for when a checkbox is changing, but value not updated yet.

See reference: 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() + ".");
}



回答2:


If you want to get the collection of all checked items use checkedListBox1.CheckedItems. To display all checked items upon clicking a button, use following:

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

If you need only their indices, use checkedListBox1.CheckedIndices instead.



来源:https://stackoverflow.com/questions/13070762/recognize-checkedlistbox-item-has-been-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!