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 selects first item in list when nothing is selected. 1

Status
Not open for further replies.

bigracefan

Programmer
Apr 2, 2002
304
US
Hi,

I'm trying to test for no value selected in a listview. This is what I have. If nothing is seleced it takes the frist item in the list.


If lvMachine.ListItems.Count > 0 Then
If lvMachine.SelectedItem.Text = True Then
Mach_Num = lvMachine.ListItems (lvMachine.SelectedItem.Index).Text
Else
MsgBox "Test"
End If
End If


Thanks,
Pete
 
I don't believe you want to be checking
If lvMachine.SelectedItem.Text = True Then

But instead want to check
If Not lvMachine.SelectedItem Is Nothing Then

This will check to see if the selecteditem is set to a listitem or nothing. I don't beleive the actual text will ever be True. Plus it would cause an error if SelectedItem was ever set to nothing, since there would be no object to get to the .Text property.
 
Thanks. I tried this but it still goes to the next line if nothing was selected. It still picks the first item.
 
How do you know that the first item isn't what is selected? I believe the ListView automatically selects the first item when the first item gets added. If you set the HideSelection to False, then it'll keep the selected item highlighted while you're not in that control (it might help you see if it's really selected).

After loading the items into the view, set the selecteditem to nothing (Set ListView1.SelectedItem = Nothing) to have the listview with no items selected.
 
Thanks, this works better. The user can see that the first item is selected.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top