Rr1n
Rr1n

Reputation: 121

C# Checkedlistbox if checked

Is it possible to apply .Checked== to checkedlistbox as in checkbox?

If to do it in a way as with checkbox it not works

if(checkedListBox1.Items[2].Checked==true)
{
}

Upvotes: 10

Views: 46532

Answers (10)

Michael Hauptvogel
Michael Hauptvogel

Reputation: 131

I ran into a similar issue: On a click of an item, the state should be converted from either checked/ non-checked to opposite. Here i post the event and the check and change:

    CheckedListBox ChkLBox;
    private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SelectedIndex = ChkLBox.SelectedIndex; //
        var Item = ChkLBox.Items[SelectedIndex];

        bool IsChecked = (ChkLBox.GetItemChecked(ChkLBox.SelectedIndex));
        ChkLBox.SetItemChecked(ChkLBox.Items.IndexOf(Item), !IsChecked);
    }

Upvotes: 0

Wulf Kreppel
Wulf Kreppel

Reputation: 1

var itemChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);

Upvotes: -1

arnab mukherjee
arnab mukherjee

Reputation: 1

checkedListBox1.CheckedItems.Count>0

Upvotes: -1

you might be looking for something like this

foreach(int i in checkedListBox1.SelectedIndices)
        {
            if(checkedListBox1.GetItemCheckState(i)!=CheckState.Checked)
            {
                ....
            }
        }

Upvotes: 0

Shilpa SK
Shilpa SK

Reputation: 11

GetItemChecked() returns a boolean value. So you can use it as the following:

if(checkedListBox1.GetItemChecked(index) == true) {

}

Where index is an integer value denoting the row index of checkedListBox1.

Upvotes: 1

Shilpa SK
Shilpa SK

Reputation: 1

GetItemCheckState() returns a boolean value. So you can use as follows:

if(checkedListBox1.GetItemCheckState(index) == true)
{

}

where index is an integer value denoting the row index of CheckedListBox

Upvotes: 0

gkb
gkb

Reputation: 1459

You can use it in this way

if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2"))
{
    MessageBox.Show("Test");
}

Upvotes: 7

Baldrick
Baldrick

Reputation: 11840

What you need is the method GetItemCheckState.

Usage as follows:

if(checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{

}

Upvotes: 12

JulieC
JulieC

Reputation: 193

Try something like...

checkedListBox1.GetItemChecked(i)

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

Upvotes: 3

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

I'm not sure i understand your question, do you want to check if at least 1 item in the listbox is checked? If so you could do that

if(checkedListBox1.Items.Any(item=>item.Checked))
{
}

Upvotes: -2

Related Questions