I'm trying to list all items in a directory which has been successful. I need to check if the item's status is "Unchecked", and if it is to give me the name of it in a variable.
TL;DR: If item is unchecked, write item in variable.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (checkedListBox1.GetItemChecked(e.Index) == true)
{
if (checkedListBox1.CheckedItems.Count != 0)
{
// If so, loop through all checked items and print results.
string s = "";
for (int x = 0; x < checkedListBox1.CheckedItems.Count; x )
{
if (checkedListBox1.CheckedItems[x] == 0)
{
s = checkedListBox1.CheckedItems[x].ToString();
}
}
MessageBox.Show(s);
}
}
}
This is my current code.
CodePudding user response:
First thing, you don't need to check things against true or false.
You don't need to do it like you are doing,
checkedListBox1.GetItemChecked(e.Index) == true
Write it like this instead and it will work just fine,
if(checkedListBox1.GetItemChecked(e.Index))
And to get all un-checked items, the following should work. You need to check if checked list contains a checkbox. If it doesn't contain it, it will be an unchecked one. Code goes below.
string s = "";
for (int x = 0; x < checkedListBox1.CheckedItems.Count; x )
{
if (!checkedListBox1.CheckedItems.Contains(CheckedItems[x]))
{
s = checkedListBox1.CheckedItems[x].ToString();
}
}
MessageBox.Show(s);
CodePudding user response:
To get a list of all unchecked items:
private void button1_Click_1(object sender, EventArgs e)
{
List<String> items = new List<String>();
for (int i=0; i<checkedListBox1.Items.Count; i )
{
if (!checkedListBox1.GetItemChecked(i))
{
items.Add(checkedListBox1.Items[i].ToString());
}
}
// ... do something with "items" ...
foreach(String item in items)
{
Console.WriteLine(item);
}
}