Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two questions about list view??

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
Hi all,

I am populating my list view with some dummy text with checkboxes on.

My questions are:

1-I am trying get the text and index for the items that have been Checked.

int totalItems=listView1.Items.Count-1;

for (int n=0;n<totalItems;n++)
{
if (listView1.CheckedItems[n].Checked==true)
{
MessageBox.Show(&quot;Selected Item Text: &quot; + listView1.Items[n].Text);
MessageBox.Show(&quot;Selected Item Index: &quot; + listView1.Items[n].Index);
}
}

I do as above. But It dispalys even un checked rows. I dont' know why???

2- How can i get only one checked row? If multiple selection is false?

regards
 
First, you have a little mistake. If you write this:
int totalItems=listView1.Items.Count-1;
then in the loop you have to write:
n<=totalItems
but, it is very important to use a variable as the meaning of it, so totalItems holds the number of items that it is the count itself, so write:
int totalItems=listView1.Items.Count;
and leave the loop as it is.
Second, there is a better way to do what you did.
If you want to discover the selected items use:
listView1.SelectedItems.Count
and
listView1.SelectedItems[n] to get the item.
You can do the same for checked items:
listView1.CheckedItems.Count
and
listView1.CheckedItems[n] to get the item.
This list contains only the items that are checked only if you set the property CheckBoxes in the list properties to True, as it is written in:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformslistviewclasscheckeditemstopic.htm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top