C# VB.net列表框删除项目
当我运行这段代码时,它会给我一个错误,表示值不能为nullC# VB.net列表框删除项目,c#,vb.net,listbox,C#,Vb.net,Listbox,当我运行这段代码时,它会给我一个错误,表示值不能为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 Li
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
错误出现在这一行
If ListBox1.Items.Contains(Item1) = True Then
此外,所有整数都已变暗
我想做的是,是否应该删除包含项目1的列表框
如果我将值设为1,然后将其更改为0,则代码将正常工作
但是如果我从一开始就给A值0,代码就会崩溃
请帮忙
谢谢你的改变
If ListBox1.Items.Contains(Item1) = True Then
到
因为当时Item1是
Nothing
,所以添加一个检查Nothing
。那么你建议如何解决@NikhilAgrawal你能帮我处理代码吗@NikhilAgrawal
If Item1 IsNot Nothing AndAlso ListBox1.Items.Contains(Item1) Then