if listbox selected item is null or empty

dwyane123

Member
Joined
Oct 14, 2008
Messages
23
Programming Experience
Beginner
Hi all, need help here,

i need to make a if condition where non of the item in he listbox is selected

i tried

if lstlistbox.selectedItem.toString = ""
'stop

else

' continue

end if
however, it returns an error "NullReferenceException"
so how is it possible for me to make the if condition where the non of the item in the listbox is selected.

Thanks alot:)
 
Hello.

ToString() Is a function like every other one, of course it's returning this exception...go with this instead:

VB.NET:
If listBox.SelectedItem IsNot Nothing AndAlso listBox.SelectedItem.ToString() <> "" Then
End If

AndAlso is a short circuit Operator, which means that if the first condition fails, he doesn't even bother to look at the other conditions (this is why ToStirng isn't failing in this case).

Bobby
 
Hey bro the If listBox.SelectedItem IsNot Nothing works well but not listBox.SelectedItem.ToString() <> "". Using the whole code you give shows the error like what i receive just now. Therefore i think i will just use " If listBox.SelectedItem IsNot Nothing " as it work well

Anw. Thanks alot for you help :)

Hello.

ToString() Is a function like every other one, of course it's returning this exception...go with this instead:

VB.NET:
If listBox.SelectedItem IsNot Nothing AndAlso listBox.SelectedItem.ToString() <> "" Then
End If

AndAlso is a short circuit Operator, which means that if the first condition fails, he doesn't even bother to look at the other conditions (this is why ToStirng isn't failing in this case).

Bobby
 
Try this:

If listBox.SelectedIndex = -1

indicates that nothing was selected.
Absolutely. Why would you have to check for an empty string? Why would you be putting empty strings into your ListBox in the first place?
 
Back
Top