如何从列表框中删除文本

时间:2014-04-25 22:24:55

标签: vb.net listbox

我正在尝试在Visual Basic中创建一个应用程序,允许用户键入文本并将其添加到列表中,以及从列表中删除项目。到目前为止我的问题是我无法删除这些项目,我可以添加它们,只是不删除它们。我的代码如下:

Public Class Form1
Public Listed As String
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)                        Handles btnAdd.Click
    Dim Prompt As String = "Enter Items To Add Here"
    Listed = InputBox(Prompt) 'Listed is the text from the input box
    lstBox.Items.Add(Listed).ToString() 'lstBox is ListBox1


End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)             Handles btnRemove.Click
    With lstBox
        .SelectedItem.Remove(Listed)
    End With
End Sub
End Class

2 个答案:

答案 0 :(得分:1)

ListBox项目没有Remove方法。您应该使用ListBox的Remove集合的Items方法。

lstBox.Items.Remove(lstBox.SelectedItem) ' Removes the currently selected item from lstBox

答案 1 :(得分:0)

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)               Handles btnRemove.Click
    With lstBox
        .Items.Remove(lstBox.SelectedItem)
      '/.Items.Remove(lstbx.SelectedItems)
    End With
End Sub