1

该字符串看起来像 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
4

2 回答 2

1

ListBox当您通过按删除项目时Button2,将SelectedIndexChanged调用 的ListBox1。在那里,所选项目将一无所有,因此要解决此问题,请SelectedIndexChanged event在分配字符串变量之前添加以下行。

    If ListBox1.SelectedItem Is Nothing Then
        Exit Sub
    End If
于 2013-07-24T23:50:58.433 回答
0

尝试这个:

listbox.selecteditem.remove()

它将删除listbox.

于 2014-08-02T08:31:28.497 回答