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?

[Webinar] Streamline your web hosting managementRegister Today

paul_tsekovConnect With a Mentor Commented:
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

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.

All Courses

From novice to tech pro — start learning today.