Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize CheckedListBox item has been selected

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"

like image 436
Pichu Avatar asked Oct 25 '12 14:10

Pichu


1 Answers

   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() + ".");
}
like image 67
urlreader Avatar answered Sep 20 '22 20:09

urlreader