如何删除vb中列表框中的项目

时间:2013-07-24 23:39:56

标签: vb.net listbox

字符串看起来像11,33,44 我将三个字符串拆分为3个文本框,然后当我执行ListBox1.Items.Remove(ListBox1.SelectedItem)时,它不起作用。

它表示ss.Split(“,”)对象引用未设置为对象的实例。

这是我的代码

    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

2 个答案:

答案 0 :(得分:1)

当您按ListBox时从Button2删除某个项目时,正在调用SelectedIndexChanged的{​​{1}}。在那里,所选项目将一无所获,因此要解决此问题,请在分配字符串变量之前在ListBox1内添加以下行。

SelectedIndexChanged event

答案 1 :(得分:0)

试试这个:

listbox.selecteditem.remove()

它将删除listbox中的所选项目。