Results 1 to 8 of 8

Thread: Remove Selected Item from ListBox

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    NJ
    Posts
    19
    I've been trying to remove the selected item from a listbox but I havn't got it yet. Any help is appreciated.

  2. #2
    Guest
    Use this:

    Code:
    Public Sub List_Remove(List As ListBox)
    On Error Resume Next
    If List.ListCount < 0 Then Exit Sub
      List.RemoveItem List.ListIndex
    End Sub
    
    Usage:
    
    Call List_Remove(List1)

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    I think he is just trying to remove the selected item only Matthew. Is that what you mean Tony900?

    Try this:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      List1.AddItem "A"
      List1.AddItem "B"
      List1.AddItem "C"
      List1.AddItem "D"
    End Sub
    
    Private Sub List1_Click()
      List1.RemoveItem List1.ListIndex
    End Sub

    If it is a multiple selecting Listbox then you can try this:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      'PURPOSE: Change Multiselect to 1 or 2 for multiple selecting
      'List1.MultiSelect = 2
    
      'PURPOSE: Test Data
      Dim int_X  As Integer
      Do
        List1.AddItem int_X
        int_X = int_X + 1
      Loop Until int_X = 11
    End Sub
    
    Private Sub Form_Click()
      'PURPOSE: If select then remove
      Dim int_TotalItem As Integer
      Do Until List1.ListCount = int_TotalItem
        If List1.Selected(int_TotalItem) = True Then
          List1.ListIndex = int_TotalItem
          List1.RemoveItem List1.ListIndex
        Else
          int_TotalItem = int_TotalItem + 1
        End If
      Loop
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  4. #4
    Guest
    That is what Matthew's code does, but his has error handling, making sure that if the ListIndex Is greater than zero, and if that still doesnt work(if you try to remove a list item without selecting it, you get an error) he has the On Error Resume Next line to make sure there are no errors.

  5. #5
    Guest
    The code does remove the selected text in the list.

    Code:
    Public Sub List_Remove(List As ListBox)
    On Error Resume Next
    If List.ListCount < 0 Then Exit Sub 'If less than 0 then exit sub
      List.RemoveItem List.ListIndex 'Remove item currently selected
    End Sub
    
    Usage:
    
    'Call List_Remove(Object)
    Call List_Remove(List1)
    It's just turned into a Public Sub, rather than written out code. So this can be used in many modules . And it's easier to understand also.

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Talking

    Sorry Matthrew!

    I thought it removes the entire list. I misunderstood. SORRY AGAIN my friend.
    Chemically Formulated As:
    Dr. Nitro

  7. #7
    Guest
    It's ok Nitro, I do that a lot to sometimes, misread the code/question/answer/etc. So if you want to remove the whole list, all you gotta do is use this code:

    Code:
    Public Sub List_Clear(List As ListBox)
    List.Clear
    End Sub
    
    Usage:
    
    Call List_Clear(List1)
    Simple as that .

  8. #8
    Guest
    I don't think it's necessary to put it in a function though. Functions are mainly used to save time when you have to work with large amounts of code that are repeated throughout different parts of the project. In this case, it will take longer to call the function than to empty the ListBox.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width