How to remove text from a list box

跟風遠走 提交于 2019-12-13 07:26:54

问题


I am trying to make an app in Visual Basic that allows an user to type in text and add it to the list as well as remove items from the list. My problem so far is that I can't remove the items, I can add them, just not remove them. My code is as follows:

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

回答1:


ListBox items do not have a Remove method. You should use the Remove method of the ListBox's Items collection.

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



回答2:


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


来源:https://stackoverflow.com/questions/23304084/how-to-remove-text-from-a-list-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!