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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView: Determine if Item was Selected 3

Status
Not open for further replies.

rennis

Programmer
Dec 6, 2006
80
CA
How is it possible to determine if a item in a listview was selected? If no item was selected then a procedure would not occur, however, if there was one then proceed.
 

Check the
ListView.ListItems(0).Selected
property

You can loop through all ListItems using
dim oListItem as ListItem
For Each oListItem in ListView.ListItems
if oListItem.Selected then

end if
next oListItem

 
Thanks SBerthold. This works, but it also works if no item was selected...

Thanks for your help though
 
My apologies, but would this apply to subitems as well?
 

I am not sure what you mean with "apply".
But you can also use a For Each-Next, (or just a basic For loop using the Index and Count values), on the ListItems and the SubItems of a ListItem:

Code:
Dim oListItem As MSComctlLib.ListItem
Dim oSubItem  As MSComctlLib.ListSubItem

For Each oListItem in myListView.ListItems
     If oListItem.Selected then
        'X
     End If

     For Each oSubItem In oListItem.ListSubItems
          'X
     Next oSubItem 
Next oListItem



 
UM you don't need to iterate through the listview testing each item.

use the SelectedItem property

so
Code:
Dim oListItem As MSComctlLib.ListItem
Dim oSubItem  As MSComctlLib.ListSubItem

Set oListItem = myListView.SelectedItem

If oListItem is Nothing then
'Nothing Selected
else
'oListItem contains valid reference to
'selected item, so we need to process appropriately
end if

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Well, you do if you think more than one item might be selected ...
 
Strongm - Correct!

If Multiselect is in use, testing the .SelectedItem property will tell you if you need to iterate through the listview to find the other selections.

The OP does imply that it is a single selection though...


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Thanks everyone! greatly appreciate your assistance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top