locked
removeat

Answers

  • Is that a question?

    You can remove the item from a given position in a ListBox like this:

    ListBox1.Items.Clear()
    ListBox1.Items.Add("aaa")
    ListBox1.Items.Add("bbb")
    ListBox1.Items.Add("ccc")
    ListBox1.Items.Add("ddd")
    ListBox1.Items.RemoveAt(2) 'removes item "ccc")
    
    


    • Proposed as answer by jwavila Sunday, August 07, 2011 11:58 PM
    • Marked as answer by Martin_XieModerator Friday, August 12, 2011 5:28 AM
    Sunday, August 07, 2011 11:43 PM
  • maybe the OP wants to know how to remove a selected item?

    in which case, instead of hardcoding the Index of the item, pass the SelectedIndex property to the RemoveAt method

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.AddRange(New String() {"aaa", "bbb", "ccc", "ddd", "eee"})
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ListBox1.SelectedIndex = -1 Then
          ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
      End Sub
    

    Monday, August 08, 2011 4:45 AM

All replies

  • Is that a question?

    You can remove the item from a given position in a ListBox like this:

    ListBox1.Items.Clear()
    ListBox1.Items.Add("aaa")
    ListBox1.Items.Add("bbb")
    ListBox1.Items.Add("ccc")
    ListBox1.Items.Add("ddd")
    ListBox1.Items.RemoveAt(2) 'removes item "ccc")
    
    


    • Proposed as answer by jwavila Sunday, August 07, 2011 11:58 PM
    • Marked as answer by Martin_XieModerator Friday, August 12, 2011 5:28 AM
    Sunday, August 07, 2011 11:43 PM
  • you can also do it like this i believe:

    ListBox1.Items.Remove(2)


    If you want something you've never had, you need to do something you've never done.
    Monday, August 08, 2011 12:58 AM
  • Hi Paul. Remove is not the same as RemoveAt. With RemoveAt, you specify the index of the item you want to remove from the list, while with Remove, you need to specify its value. ListBox1.Items.Remove(2) would not remove anything from the list in the code I posted.  To use Remove to remove the third item, you would code

    ListBox1.Items.Remove("ccc")

    Monday, August 08, 2011 1:21 AM
  • maybe the OP wants to know how to remove a selected item?

    in which case, instead of hardcoding the Index of the item, pass the SelectedIndex property to the RemoveAt method

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.AddRange(New String() {"aaa", "bbb", "ccc", "ddd", "eee"})
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ListBox1.SelectedIndex = -1 Then
          ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
      End Sub
    

    Monday, August 08, 2011 4:45 AM
  • Thanks all for your help and good suggestions.

    Hi Rafael,

    Welcome to the MSDN forum.

    If we misunderstood you or you still have any doubt about this question, please kindly describe your question in more detail.


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 12, 2011 5:31 AM