|
-
Aug 30th, 2005, 03:05 AM
#1
Thread Starter
Lively Member
[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
Last edited by Astro; Aug 30th, 2005 at 03:20 AM.
-
Aug 30th, 2005, 03:19 AM
#2
Frenzied Member
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
-
Aug 30th, 2005, 03:38 AM
#3
Thread Starter
Lively Member
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
-
Aug 30th, 2005, 04:24 AM
#4
Frenzied Member
Re: Loop to add list box items
 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
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
|