im trying to determine if there is an item selected on the listview..

i have a listview that displays customers name with a .tag that holds there ID.

how do you tell if the tag is 0 or empty before executing code?

in vb6 i simply did

if .lstwhatever.selecteditem is nothing then exit sub

in vb.net ive tried multiple things

if .lstwhatever.selecteditems.item(0) is nothing then exit sub
if .lstwhatever.selecteditems is nothing then exit sub
if .lstwhatever.selecteditems.item(0).tag = 0 then exit sub

the code i want to execute if there is a tag (or atleast a listitem is selected)  is down here.

anyone have any ideas.

thanks

You want to get the selecteditems count...

If ListView1.SelectedItems.Count = 0 Then Exit Sub

no, not the count.

i always thought that if a listview had anything in it, that it would automatically have a default index selected. but i run into an error where it doesn't from time to time, and like i said i need to make sure that there is a .tag set on the listview before i can execute certain code.

vb6 was quite easy, just is listview.selecteditem is nothing then exit sub

not so easy in vb.net.

i found a work around by saving the .tag in a variable and then just using the variable instead of getting the listviews .tag each time - it isn't really the route that i wanted to take however.

Hmmm guess I misunderstood...still not sure. Are you trying to test for BOTH if there's a selected item and if it has a tag? Like this...

If ListView1.SelectedItems.Count = 0 OrElse ListView1.SelectedItems(0).Tag Is Nothing Then
            Exit Sub
        End If
lstview.selecteditems(0).tag is nothing then exit sub

invalidargument=value of '0' is not valid for 'index'. parameter name: index

but yes this is exactly what im trying to determine, if the tag = nothing

invalidargument=value of '0' is not valid for 'index'. parameter name: index

Right that's why I have both tests in the line. You have to make sure selecteditem count is > 0 before trying to access the first item. It should work with the full code I posted, or just

If ListView1.SelectedItems.Count = 0 OrElse ListView1.SelectedItems(0).Tag Is Nothing Then Exit Sub
If Me.lstResults.Items.Count = 0 OrElse lstResults.SelectedItems(0).Tag Is Nothing Then Exit Sub

i added

lstresults.selecteditems.item(0).tag

both times got:

InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index

exactly what im doing:
i get the error by populating the list with customer names + id as the tag, i click in the white space bellow the last index of the list (after the last line). when i know nothing is selected, i click the command button the code that i want to execute only if there is a valid tag.

selecteditems.count sorry i didnt look at your code right.

works perfectly now, thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.