如何在vb中删除列表框中的项目 [英] how to remove a item in listbox in vb

查看:585
本文介绍了如何在vb中删除列表框中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串看起来像11,33,44我把三个字符串分成三个文本框,然后当我做ListBox1.Items.Remove(ListBox1.SelectedItem)时,它不起作用.

the string is looks like 11,33,44 i made a split into three strings into 3 textboxes, and then when i do ListBox1.Items.Remove(ListBox1.SelectedItem) it doesn't work.

它说ss.Split(,")对象引用未设置为对象的实例.

it says ss.Split(",") Object reference not set to an instance of an object.

这是我的代码

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As        System.EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim ss As String = ListBox1.SelectedItem

    Dim aryTextFile(2) As String
    aryTextFile = ss.Split(",")


    TextBox1.Text = (aryTextFile(0))
    TextBox2.Text = (aryTextFile(1))
    TextBox3.Text = (aryTextFile(2))

    ss = String.Join(",", aryTextFile)

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    ListBox1.Items.Add(TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text)
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    ListBox1.Items.Remove(ListBox1.SelectedItem)


End Sub

推荐答案

通过按 Button2 ListBox 中删除项目时, SelectedIndexChanged < ListBox1 的/code>被调用.在那里,所选项目将一无所有,因此要解决此问题,请在分配字符串变量之前在 SelectedIndexChanged事件中添加以下行.

When you remove an item from the ListBox by pressing the Button2, the SelectedIndexChanged of the ListBox1 is being called. There, the selected item will be nothing, so to solve this, add the following lines inside the SelectedIndexChanged event before assigning the string variable.

    If ListBox1.SelectedItem Is Nothing Then
        Exit Sub
    End If

这篇关于如何在vb中删除列表框中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆