Results 1 to 12 of 12

Thread: Removing Empty Items in a listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87

    Removing Empty Items in a listbox

    does anyone know how to remove empty items in a listbox? here's an example of what is in my listbox...

    #1
    #2
    (EMPTY SPACE)
    #3
    #4
    (EMPTY SPACE)
    #5

    i want to remove those empty space, can someone help?

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Hmm..

    Probably would have to loop it and check per item index and check:

    If Not Len(Trim(List1.List(Index))) Then ... 'fast compare, no string compare(string comp = slower)

    Then you use RemoveItem(Index).
    Last edited by DiGiTaIErRoR; May 25th, 2004 at 11:29 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87
    thanks, works perfectly. i just didnt know how to check if an item was empty

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87
    nevermind, i thought i had it working, but i didnt. here's what i have so far.

    Code:
    Dim strCompare As String
    Dim intCompare As Integer
    For i = 0 To List1.ListCount - 1
    strCompare = List1.List(i)
    intCompare = Len(Trim(strCompare))
    If intCompare = 0 Then 'if zero, it will remove it
    list1.removeitem(i)
    End If
    Next i
    i got a run time error 5
    Last edited by DangerousNiNjA; May 26th, 2004 at 12:19 AM.

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    On what line?

  6. #6
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    VB Code:
    1. For i = 0 To List1.ListCount -1
    2.    If list1(i) = "" Or List1(i) = " " Then List1.RemoveItem i
    3. Next i

    I think that works...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87
    it highlights this line: List1.RemoveItem (i)


    and Spajeoly, thanks for the help, but i get the same error


    i think it's because when you remove an item from a listbox, the number of item changes. "for i = 0 to list1.listcount -1" counts the listbox at the beginning, but when you remove an item, the number of items changes. but i still dont know how to fix it...

  8. #8
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    change

    VB Code:
    1. If list1(i) = "" Or List1(i) = " " Then List1.RemoveItem i

    to

    [Highlight=VB]
    If trim(list1(i)) = "" Then List1.RemoveItem i
    [Highlight=VB]

    That prevents any number of spaces being entered

    VB Code:
    1. Do until I > list1.listcount -1
    2.  
    3.        'If deleted dont add 1
    4.  
    5. '        If not deleted add 1 to I
    6. Loop
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    the easiest way to solve it is to just go the other way through the list (start at the end and work towards the start)
    VB Code:
    1. Dim strCompare As String
    2. Dim intCompare As Integer
    3. For i = [b]List1.ListCount - 1 To 0 [i]Step -1[/i][/b]
    4.   strCompare = List1.List(i)
    5.   intCompare = Len(Trim(strCompare))
    6.   If intCompare = 0 Then 'if zero, it will remove it
    7.     list1.removeitem(i)
    8.   End If
    9. Next i

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87
    thanks everyone, works perfect

  11. #11
    Hyperactive Member
    Join Date
    Mar 2014
    Posts
    321

    Re: Removing Empty Items in a listbox

    Code:
    Private Sub Command1_Click()
    '''''''''' simple line to remove found item add text1.text to search
    Dim i
    For i = 0 To List1.ListCount - 1
    If InStr(List1.List(i), Text1.Text) Then
    List1.RemoveItem (i)
    End If
    Next i
    End Sub
    
    Private Sub Command2_Click()
    '''''''''''''''''''''''''''''' use this to remove blank emty lines after it removed item
    Dim i
    For i = 0 To List1.ListCount - 1
    If InStr(List1.List(i), Text1.Text) Then
    List1.RemoveItem (i)
    End If
    Next i
        Dim strCompare As String
        Dim intCompare As Integer
        For i = List1.ListCount - 1 To 0 Step -1
          strCompare = List1.List(i)
          intCompare = Len(Trim(strCompare))
          If intCompare = 0 Then 'if zero, it will remove it
            List1.RemoveItem (i)
          End If
        Next i
    
    End Sub
    
    Private Sub Form_Load()
    List1.AddItem "holla"
    List1.AddItem "glue"
    List1.AddItem "aggass"
    List1.AddItem "smile"
    End Sub

    add text1.text and list1

    text1.text is to search and remove

  12. #12
    Administrator Steve R Jones's Avatar
    Join Date
    Apr 2012
    Location
    Clearwater, FL.
    Posts
    2,349

    Re: Removing Empty Items in a listbox

    cobraide - Please note that this thread is TEN YEARS OLD and that a solution was already found.

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