ListView troubles...

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
Hi all,

is there an easy way to access a single selected item on a listview? i find that when i try to reference it using various methods, its always unreliable or crashing on me. for instance if i try

VB.NET:
TheId = lstEquipment.SelectedItems(0).Text

sometimes it will work, but other times its saying that 0 is not a valid index. if i dont supply anything it says that selecteditems doesnt accept that number of arguments. wouldnt 0 be a valid index, as 0 would represent teh first item in the selecteditems collection?

another method i thought of was just to use a function to search for the selected item,

ie.
VB.NET:
dim c as integer=0
For each i as listviewitem in lvEquipment.items
       if not i.selected
               c+=1
       end if
 
next
 
return c

now for an unknown reason sometimes it just returned the wrong index.

this is pretty frustrating, and if anyone could shed some light on the situation id be greatly appreciative.

regards
adam
 
hi and thanks for your reply,



im not using a listbox (although they are easier), i am actually using a listview. a bit different (and i think much more flexible) than a listbox. a listview does not have the properties selecteditem and selectedindex. (only selecteditems and selectedindicies)

still, any thoughts? gah this is so frustrating. :mad:

cheers
adam
 
I think your code

VB.NET:
TheId = lstEquipment.SelectedItems(0).Text

crash is because you click outside of the lstEquipment. May be try on this one?

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] TheId [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] obj [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = ListView1.SelectedItems
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsDBNull(obj) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]TheId = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](obj(0), ListViewItem).Text
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
 
hi ling,

i just tried your code but i had to modify it a bit... here it is

VB.NET:
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] id [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]id = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] obj [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = lvEquipment.SelectedItems
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsDBNull(obj) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]id = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](obj, ListView.SelectedListViewItemCollection).Item(0).Text
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]

it actually works for the first click of the listview, but not the second. *sigh*... i wish listviews were easier to work with.

any thoughts guys (or girls)?
[/SIZE]
 
Sorry, my mistake,

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] TheId [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] obj [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = ListView1.SelectedItems
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsDBNull(obj) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](obj, ListView.SelectedListViewItemCollection).Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]TheId = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](obj, ListView.SelectedListViewItemCollection).Item(0).Text
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
 
oh ok, that seemed to work perfectly. thanks for that!!

seems a pain in the ass to do this every time you just want to select a single item. i wonder why they didnt make an easier way?

oh well... if anything goes pear shaped i shall be back here posting my heart away :p

cheers for your help mate
have a good one
 
Here are three alternatives:
VB.NET:
        If ListView1.SelectedItems.Count > 0 Then
            Dim TheText As String = ListView1.SelectedItems(0).Text
        End If

        For Each lvi As ListViewItem In ListView1.SelectedItems
            Dim TheText As String = lvi.Text
        Next

        For i As Integer = 0 To ListView1.SelectedItems.Count - 1
            Dim TheText As String = ListView1.SelectedItems(i).Text
        Next
 
Back
Top