If ListBox1.Items(0).ToString = Nothing Then
Hi i keep getting an error here and cant seem to fix it :/
VB CODE
Code:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If ListBox1.Items(0).ToString = Nothing Then
MsgBox("this is nothing")
Else
MsgBox("something is there")
End If
End Sub
ERROR:
System.ArgumentOutOfRangeException was unhandled
Message="InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index"
Now i know that the error means that there there is nothing in "0" but my code above should check that but its not :/
What have i done wrong ?
Thanks for any help
Re: If ListBox1.Items(0).ToString = Nothing Then
Try: If ListBox1.Items.Count = 0 Then
Re: If ListBox1.Items(0).ToString = Nothing Then
vb Code:
If ListBox1.Items.Count = 0 then
'no items
Re: If ListBox1.Items(0).ToString = Nothing Then
The actual problem is you are calling .ToString on ListBox1.Items(0).
When Listbox1.items(0) is nothing, you can't call a method on it, because its not a valid instance of an object.
Code:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If ListBox1.Items(0) is Nothing Then
MsgBox("this is nothing")
Else
MsgBox("something is there")
End If
End Sub