[RESOLVED] Loop to add list box items
Hello there,
I have attmpted a For To Next Loop and a Do While Not Loop to add all the items from my list box.
Here's a look at both loops.
1)
VB Code:
Private Sub List0_DblClick(Cancel As Integer)
Dim s As Integer
For s = 0 To List0.ListCount
List1.AddItem List0.ItemData(s)
Next s
End Sub
This loop adds item as desired but prompts an error message at the end.
Run-time error 94 Invalid use of Null.
2)
VB Code:
Private Sub List0_DblClick(Cancel As Integer)
Dim i As Integer
i = List0.ListCount
Do While Not i = 0
i = i - 1
List1.AddItem List0.ItemData(i)
Looo
End Sub
This loop adds item from bottom to top since it starts from ListCount's value and - 1.
I'll like to have my items added the correct way without the error, can anyone comment on my loops and give suggestions?
will be more than glad to learn new ways of looping.
Thank You
Astro
Re: Loop to add list box items
VB Code:
Private Sub List0_DblClick(Cancel As Integer)
Dim s As Integer
For s = 0 To List0.ListCount-1
List1.AddItem List0.ItemData(s)
Next s
End Sub
Re: Loop to add list box items
VB Code:
Dim i As Integer
Dim x As Integer
i = List0.ListCount
x = 0
Do While Not x = i
List1.AddItem List0.ItemData(x)
x = x + 1
Loop
I got it too! Haha just saw your reply moinkhan.
Your's definitely shorter. Any pros/cons besides length of code?
Comments appreciated =P
Astro
Re: Loop to add list box items
Quote:
Originally Posted by Astro
VB Code:
Dim i As Integer
Dim x As Integer
i = List0.ListCount
x = 0
Do While Not x = i
List1.AddItem List0.ItemData(x)
x = x + 1
Loop
I got it too! Haha just saw your reply moinkhan.
Your's definitely shorter. Any pros/cons besides length of code?
Comments appreciated =P
Astro
I dont know about any performance difference.. but as u can see.. the lower and upper bounds of loop are set at the start of loop.. it should be faster...
This i know for sure.. Do While has to check the condition everytime...
and... my coding style suggests that this job is for For..Next... Do While is for other complex conditions... for counter based loops.. use For..Next