user1957558
user1957558

Reputation: 53

Check CheckedListBox?

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

Answers (3)

Tim Schmelter
Tim Schmelter

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

Vishal Suthar
Vishal Suthar

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

VladL
VladL

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

Related Questions