vb.net 如何在vb中删除列表框中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17846668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove a item in listbox in vb
提问by Tony Yang
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.
字符串看起来像 11,33,44 我将三个字符串拆分为 3 个文本框,然后当我执行 ListBox1.Items.Remove(ListBox1.SelectedItem) 时它不起作用。
it says ss.Split(",") Object reference not set to an instance of an object.
它说 ss.Split(",") 未将对象引用设置为对象的实例。
here is my code
这是我的代码
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
回答by David -
When you remove an item from the ListBoxby pressing the Button2, the SelectedIndexChangedof the ListBox1is being called. There, the selected item will be nothing, so to solve this, add the following lines inside the SelectedIndexChanged eventbefore assigning the string variable.
当您删除从一个项目ListBox按Button2的SelectedIndexChanged的ListBox1被调用。在那里,所选项目将什么都没有,因此要解决此问题,请SelectedIndexChanged event在分配字符串变量之前在其中添加以下行。
If ListBox1.SelectedItem Is Nothing Then
Exit Sub
End If
回答by Azoz
Try this:
尝试这个:
listbox.selecteditem.remove()
It will remove the selected item in the listbox.
它将删除listbox.

