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)
{
}
What you need is the method GetItemCheckState
.
Usage as follows:
if(checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{
}
You can use it in this way
if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2"))
{
MessageBox.Show("Test");
}
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() + ".");
}
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
.
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
you might be looking for something like this
foreach(int i in checkedListBox1.SelectedIndices)
{
if(checkedListBox1.GetItemCheckState(i)!=CheckState.Checked)
{
....
}
}
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);
}
var itemChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);
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))
{
}