Zyzz Shembesh
Zyzz Shembesh

Reputation: 220

VB.net listBox removing items

when i run this code it gives me an error which says value cannot be null

If (A >= 1) Then
    Item1 = fish
    ListBox1.Items.Add(Item1)
Else
    If (A = 0) Then
        If ListBox1.Items.Contains(Item1) = True Then
            While ListBox1.Items.Contains(Item1) = True
                ListBox1.Items.Remove(Item1)
            End While
        End If
    End If
End If

the error comes at this line

If ListBox1.Items.Contains(Item1) = True Then

also all integers are dim-ed already

what i am trying to do is if the listbox contain item 1 should be deleted

the code work if i gave A value of 1 then changed it to 0

but if i gave the A value of 0 from the beginning the code will crash

please help

thank you

Upvotes: 0

Views: 382

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

Change

If ListBox1.Items.Contains(Item1) = True Then

to

If Item1 IsNot Nothing AndAlso ListBox1.Items.Contains(Item1) Then

Upvotes: 2

Related Questions