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

Detect when listbox has no selected item

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi!

I have a lsitbox which in turn stores logs. These logs can be added, amended and deleted. Works like a charm. But sometimes my users want to delete the logs and they click one time to many... Which in turn results in that there is no selecteditem, and hence no index which give me an error. This is all understandable BUT how can I check the listview1.selecteditem.index for not having an item selected?!

This give me an error, the index is never 0.
I also tried with: ListView2.SelectedItem.Index = vbnullstring,
I did not work either also tried with NULL but same as before does not work, this is my code.


Code:
With UserForm1.ListView2
If ListView2.SelectedItem.Index = 0 Then

MsgBox ("You have tried to delete a log that is NOT there!")

Else

logg = ListView2.SelectedItem.Index

End If
 
What about this ?
If ListView2.SelectedItem.Index [!]<[/!] 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
..or
If ListView21.SelectedItem Is Nothing Then

combo
 
Hey!

It does not work with

If ListView2.SelectedItem.Index < 0 Then

Cause the objectvariable or with variable has not been set(freely translated from swedish)

If ListView21.SelectedItem Is Nothing Then
does not work since it returns "" and that is not the same as nothing i presume.. ?!

any other ideas.. Im searching the net as a crazy for a solution!

 
For me [tt]If ListView2.SelectedItem Is Nothing Then[/tt]
works (obviously ListView2 instead of suggested ListView21) when you work with MSComctlLib ListView and the list is empty. It does not return "", the code checks if object exists (ListItem here) and is equivalent to [tt]ListView2.ListItems.Count[/tt].



combo
 

Before you do any action with your listbox/ListView (which is it?) can you loop thru the items and detect if any are selected? And if you do not detect any that are selested - display a message to the user about it, and don't take any action.

Just a suggestion...

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top