|
-
May 25th, 2004, 11:10 PM
#1
Thread Starter
Lively Member
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?
-
May 25th, 2004, 11:26 PM
#2
So Unbanned
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.
-
May 26th, 2004, 12:01 AM
#3
Thread Starter
Lively Member
thanks, works perfectly. i just didnt know how to check if an item was empty
-
May 26th, 2004, 12:10 AM
#4
Thread Starter
Lively Member
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.
-
May 26th, 2004, 02:10 AM
#5
So Unbanned
-
May 26th, 2004, 02:21 AM
#6
Frenzied Member
VB Code:
For i = 0 To List1.ListCount -1
If list1(i) = "" Or List1(i) = " " Then List1.RemoveItem i
Next i
I think that works...
-
May 26th, 2004, 02:30 AM
#7
Thread Starter
Lively Member
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...
-
May 26th, 2004, 02:56 AM
#8
KING BODWAD XXI
change
VB Code:
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:
Do until I > list1.listcount -1
'If deleted dont add 1
' If not deleted add 1 to I
Loop
-
May 26th, 2004, 03:57 AM
#9
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:
Dim strCompare As String
Dim intCompare As Integer
For i = [b]List1.ListCount - 1 To 0 [i]Step -1[/i][/b]
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
-
May 26th, 2004, 04:18 AM
#10
Thread Starter
Lively Member
thanks everyone, works perfect
-
Apr 1st, 2014, 05:40 AM
#11
Hyperactive Member
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
-
Apr 1st, 2014, 08:10 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|