How to check if listview item has been checked

I want to check if a checkbox has been checked in a listview control before proceeding.  if none are checked then I don't want to carry on

foreach (ListViewItem item in lvtest.Items)

How can I do this?
Who is Participating?
VBRocksConnect With a Mentor Commented:
       Dim bIsChecked As Boolean = False

        For Each item As ListViewItem In Me.ListView1.Items

            If item.Checked = True Then

                bIsChecked = True
                Exit For

            End If


        If bIsChecked Then

            'At least one was checked...

        End If

Todd GerbertConnect With a Mentor IT ConsultantCommented:
This is essentially the same thing VBRocks wrote, but in C#.
bool anyChecked = false;
foreach (ListViewItem item in listView1.Items)
	if (item.Checked)
		anyChecked = true;
if (anyChecked)
	// Proceed
	// None were checked
	MessageBox.Show("You must select at least one item.");

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.