【问题标题】:VB.net listBox removing itemsVB.net listBox 删除项目
【发布时间】:2015-03-19 13:55:08
【问题描述】:

当我运行这段代码时,它给了我一个错误,说值不能为空

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 项应该被删除

如果我给 A 值 1 然后将其更改为 0,则代码有效

但如果我从一开始就将 A 值设为 0,代码将崩溃

请帮忙

谢谢

【问题讨论】:

  • 因为当时 Item1 是Nothing 所以添加一个检查Nothing。
  • 那么你有什么建议可以解决@NikhilAgrawal
  • 你能帮我写代码吗@NikhilAgrawal

标签: c# vb.net listbox


【解决方案1】:

改变

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

到

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多