Reputation: 53
What is the code to check if anything is checked in a CheckedListBox
. I am making an application to register Movies, and I need to check the Genres of the movies from the CheckedListBox
. If nothing is checked, a MessageBox
should appear telling you You need to select a Genre for the movie.
Upvotes: 0
Views: 179
Reputation: 460298
You could simply use the SelectedIndex
property:
if(checkListBoxGenre.SelectedIndex == -1)
{
MessageBox.Show("You need to select a Genre for the movie.");
}
Another option is to use the Text
property which gets the text of the currently selected item in the ListBox.
if(checkListBoxGenre.Text.Length == 0)
{
MessageBox.Show("You need to select a Genre for the movie.");
}
It's just a matter of readability and personal preferences.
Upvotes: 1
Reputation: 17194
if(checkedListBox1.CheckedItems.Count != 0)
{
// If so, loop through all checked items and print results.
}
else
{
MessageBox.Show("You need to select a Genre for the movie.");
}
Upvotes: 0
Reputation: 13043
You should check CheckedItems
or CheckedIndices
properties, depending on what you need
CheckedListBox cl = new CheckedListBox();
if (cl.CheckedIndices.Count == 0)
{
MessageBox.Show("You need to select a Genre for the movie.");
}
Upvotes: 1