在列表框中,我想删除一个项目,但保留其他项目。 我已经尝试过这段代码,但它没有用。
ListBox1.Items.Remove(ListBox1.Items.IndexOf("TEST"))
用Google搜索但无法找到工作方法。 任何帮助表示赞赏。
答案 0 :(得分:2)
这适用于WebForms
首先找到您要删除的ListItem
:
Dim liItem As ListItem = ListBox1.Items.FindByValue("TEST")
你也可以通过Text找到而不是值:
Dim liItem As ListItem = ListBox1.Items.FindByText("TEST")
然后删除它:
If ( liItem IsNot Nothing) Then
ListBox1.items.Remove(liItem)
end If
更新 - 这适用于Windows窗体
ListBox1.Items.RemoveAt(ListBox1.Items.IndexOf("TEST"))
或者这个:
Dim index As Integer = ListBox1.FindString("TEST")
If ( index <> -1 ) Then
ListBox1.Items.RemoveAt(index)
End If