List Box

Ok, I am haveing a problem with a list box. I need to make a program that reads a list box line by line. What I have used is:
----------------------------
list1.listindex = 0
Do
list1.listindex = int(list1.listindex + 1)
Loop until List1.listindex = List1.listcount -1
------------------------------

That code works kind of but if there is only 1 string it errors and it does not read the last item if there is multiple things. I need code that can read 1 item if thats all that is in the list and one that if there is multiple items it will include the last string. Thanks

vbscript23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kamallCommented:
Put one list box and two command buttons on a form and enter the following code:

Private Sub Command1_Click()
    For x = 1 To 5
    List1.AddItem "Item" & CStr(x)
    Next
End Sub

Private Sub Command2_Click()
    For x = 0 To List1.ListCount
    Print List1.List(x)
    Next
End Sub

Regards.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
szzjjjCommented:
Your code is too complex.
Since you want to read the listbox
lineby line,you should write the
code below

for i = 0 to List1.listcount-1
item = List1.list(i)
next i
0
kamallCommented:
"Your code is too complex." ha ha ha ha.... this is really funny!
What is the difference between my code and yours???
The first part of my code is used to fill the list box (for demo). The second part does the trick.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

eab111098Commented:
you get an error because your assigning a value to listindex beyond the bounds of the listbox in your first statement inside the do loop. you cannot read the last entry in a muli-lined listbox because your loop only loops to the last entry and drops out - which means the last entry is never processed inside the loop.

since kamall seems to be a sarcastic SOB, i would reject his answer and email szzjjj and congratulate him on a fine answer.

ed.
0
kamallCommented:
eab,
Before saying these ......., have you tried my code?
Try it first, and then say what you want.
If it does not work, then, for sure your VB sucks!

vbscript23,
Please, first try my code and then decide for whome you shall give the points.
0
eab111098Commented:
kamall, well lets just analyze your code for a moment.....

this is the code you provided....

Private Sub Command1_Click()
    For x = 1 To 5
    List1.AddItem "Item" & CStr(x)
    Next
End Sub

Private Sub Command2_Click()
    For x = 0 To List1.ListCount
    Print List1.List(x)
    Next
End Sub

in the command1_click() event you populate the list with item1/2/3/4/ and 5 beginning at subscript zero. and then in command2_click event you loop thru beginning your counter at zero and end it with the value from listcount. listcount has a value of 5, however, the subscripts are number from zero to four. this means that your solution will generate a "subscript out of range" error when it tries to access subscript number 5, which doesn't exist. you forgot to subtract one from listcount in your for/next loop structure. don't feel bad, its a common mistake among beginners. curb your sarcasm until you become a competent coder.

ed.
0
kamallCommented:
eab,
I ALREADY know that.
The code WILL NOT generate an error. Thats why I said "try it first".
Don't let your mind 'stick' with the theory only.
And yes, I am a competent coder. If I wasn't, I have had put a '-1' in ListCount line. But I know the secrets!
BTW, don't forget to check out my profile.
Regards.
0
eab111098Commented:
kamall, you're right and i am wrong - particially. i don't believe i have tried to access the "listcount" item before and assumed (incorrectly as you pointed out) that it would generate an error. secrets? i don't think this is a secret. in my opinion its a developmental error. we differ on this point. enuf said. i do appologize for calling you a sarcastic sob. it seems i have been found guilty of reading your response out of its intended context.

later.
ed.
0
kamallCommented:
eab,
I appreciate your comment.

vbscript23,
Where are you man?
Please come in and grade the answer.

Regards to all.
0
kamallCommented:
vbscript23,
are you alive???
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.