VB Comparing strings with list items

How do I compare a certain string with items in a listbox?  Say for instance, the listbox had an item, say, a pencil, and it costs 2 dollars (exaggerating), but however, how I assign the 2 dollar price tag to the listbox, and have it displayed in a lblTotal label, which can keep track of other items purchased in the listbox?  Thanks
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.

I think you can use a CASE like this:

Private Sub List1_Click()

Select Case List1.ListIndex
Case 0
lblTotal =val(lblTotal) + 200
case 1
lblTotal =val(lblTotal) + 100
End Select

End Sub

Now, lets asume that you have 2 items in the list box, so if you press the first one "pencil" this will enter the case ( because List1.ListIndex will be 0) and will add the price, in this 200 to the total

I hope this can help you...
You can add numerical values to every items to a list in the ItemData property. You can store the price tag there. Let's say "pencil" is the first item then you can store 200 in ItemData(0). Getting the price tag from the selected item is easily done using code like

lblTotal = lblTotal + List1.ItemData(List1.ListIndex)

Check your help file for more info on the ItemData property.
I would suggest you consider the ListView to store multiple items; however, a quick fix would be to load an array with the prices for each item in the list box.

As you populate the list box have a global array. Set this in a module:

Global gl_prices() as Long

Then in your routine that loads up the Listbox:

Dim i_count as Integer


i_count = 0

Do Until RS.EOF

i_count = i_count + 1


Dim Preserve gl_prices(0 to i_count - 1)

gl_prices(i_count) = RS!price



Then when you get to the part where you check the item in the Listbox:

me.txt_price = gl_prices(me.List1.ListIndex)

me.txt_total = gl_prices(me.List1.ListIndex) + Clng(me.txt_total)

I assume you set the default value of the txt_price textbox to 0.

The total should add up the item prices.

If you are not using whole numbers, I suggest you change the array to a real type rather than a long integer. I am going by your example that it is a integer type of number you are saving.

Each ListBox Conrol has the Property

In Design Mode you can set the ItemData
Property for each Item in the ListBox.
For example if you have the Items:
You can assign the ItemData Property
for each of the Items as follows:
If your ListBox is named MyList then
in MyList_Click() you can use this:

Private Sub MyList_Click()
'So each time you click an item from the ListBox this code will add it's price in the Caption of the lblT
and accumulate the total price!!!    

End Sub

Look the ItemData Property it is very usable in many cases.

              Pavel Tsekov
        email : paul_tsekov@yahoo.com


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
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.