Click to See Complete Forum and Search --> : Listview/Image, a couple questions


cgchris99
August 19th, 2002, 11:20 AM
I wanted to be able to tell which Item/Image was selected in a ListView. I have the multiselect set to false. I only want them to pick one item at a time.

So here is my code

test=iew.SelectedIndices(0)

This works only the first time they pick one. If they pick a different item in the list, it throws an exception.

Any ideas?

Second question.
Can I keep the selected image from turning blue? Or is there a way to have it display a different image for the selected one?

Thanks

evschu
August 19th, 2002, 02:41 PM
try:
test = iew.selecteditems.item(0)

basically, what you are doing is saying that you want all of the selected items, then a single selected item, then the 0 says that you want the first selected item. if you wanted the second selected item you would write:

test = iew.selecteditems.item(1)

in this case, test must be dim as a listviewitem. then your code would look like:
dim test as listviewitem = iew.selecteditems.item(0)

if you want the text of the item, then dim test as string and write:
dim test as string = iew.selecteditems.item(0).text

if you want the index of the first selected item you would write:

dim test as integer = lvwresults.SelectedIndices.item(0)


As far as your second question goes, you can use checkboxes, but i haven't played around with it much.

have fun

cgchris99
August 19th, 2002, 03:20 PM
The dim statement produce an exception: specified argument out of range when you make your second selection

here is the code

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

Dim test3 As ListViewItem = ListView1.SelectedItems.Item(0)

If test3.Selected = True Then
test = test3.Index ' index number selected
End If


This works fine the first time you select and item in the list. The second time you select an item in the list you get the exception. I only want ONE item selected at a time. This is why I have multipleselects set to false.

Thanks for any advice

DSJ
August 19th, 2002, 03:40 PM
SelectedIndexChanged is getting called twice after an item is selected... Once when the old item is no longer selected and again when the new item is selected... try this:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim li As ListViewItem

If ListView1.SelectedItems.Count <> 0 Then
li = ListView1.SelectedItems(0)
MsgBox(li.Text)
Else
'MsgBox("Selected Lost Focus")
End If
End Sub
End Class

evschu
August 19th, 2002, 03:43 PM
i have a listview in a program that i am working on that is somewhat similar. i remember having a similar problem. i think what i did what change the event from selectedindex_change to a Click event. try this. here is the code that i have in my program, i hope it helps. what is does is get the name of the selected playlist in the listview (multi-select is false).

Private Sub lvwSavedPlaylists_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwSavedPlaylists.Click
Call AddPlayListDetails(lvwSavedPlaylists.SelectedItems(0).Text)
Panel1.Text = "Ready"
End Sub