[RESOLVED] add item to combo box and related price
Hi again guys. :wave:
I've been developing an application for a friend just for fun and practice(I'm just beginning as you can tell right away) and I have a combobox for the services.
I've made a select case procedure to make the price of each initial service shows on a label each time the user clicks a selection. So okay, everything works fine.
What I want to accomplish though is to make the user be able to add a service(I know this part) and a price for such a service.
Is there a method to make the price show on the label or do I have to make a database for them? (No clue about databases, yet!)
Sorry if it looks like a silly question.
Re: add item to combo box and related price
You can use the Itemdata property of thre combo to store the price.
vb Code:
Combo1.AddItem "A Service"
Combo1.ItemData(Combo1.NewIndex) = "1.98"
Combo1.AddItem "Another Service"
Combo1.ItemData(Combo1.NewIndex) = "5.67"
Re: add item to combo box and related price
And to display it on the label during run time? I mean, Each time the user clicks a new service on the combo box the label has to display the price.
Re: add item to combo box and related price
So I did that
Code:
Private Sub Command1_Click()
Combo1.AddItem "A new service"
Combo1.ItemData(0) = "1.98"
Label3 = Combo1.ItemData(0)
End Sub
but in the label it only shows a rounded number. :S
Re: add item to combo box and related price
Quote:
Originally Posted by pukisoft
but in the label it only shows a rounded number. :S
What do you mean a rounded number? As i 1.98 becomes 2 or does it chop off the decimal?
If so, you could do this with the code above:
Code:
Private Sub Command1_Click()
Combo1.AddItem "A new service"
Combo1.ItemData(0) = "1.98"
Label3.Caption = FormatCurrency(Combo1.ItemData(0),2)
End Sub
Re: add item to combo box and related price
The ItemData property can only store Integers. One option is to multiply the value by 100 when it is stored and obviously divide by 100 when the value is retrieved.
Code:
Option Explicit
Private Sub Form_Load()
Dim sngValue As Single
sngValue = 1.98
Combo1.AddItem "A new service"
Combo1.ItemData(0) = sngValue * 100
Combo1.ListIndex = 0
End Sub
Private Sub Combo1_Click()
Label1.Caption = FormatCurrency(Combo1.ItemData(0) / 100, 2)
End Sub
Re: add item to combo box and related price
Sorry my mistake. ItemData is stored as a Long Integer so it will convert the strings that I added to whole numbers. Do this instead.
vb Code:
Dim strPrice() As Currency
ReDim strPrice(0)
Combo1.AddItem "A Service"
strPrice(0) = 1.98
Combo1.AddItem "Another Service"
ReDim Preserve strPrice(UBound(strPrice) + 1)
strPrice(UBound(strPrice)) = 5.67
Label1.Caption = strPrice(0)
Re: add item to combo box and related price
Quote:
Originally Posted by brucevde
The ItemData property can only store Integers. One option is to multiply the value by 100 when it is stored and obviously divide by 100 when the value is retrieved.
Code:
Option Explicit
Private Sub Form_Load()
Dim sngValue As Single
sngValue = 1.98
Combo1.AddItem "A new service"
Combo1.ItemData(0) = sngValue * 100
Combo1.ListIndex = 0
End Sub
Private Sub Combo1_Click()
Label1.Caption = FormatCurrency(Combo1.ItemData(0) / 100, 2)
End Sub
Nice idea.
Re: [RESOLVED] add item to combo box and related price
Thnkx guys. now I have two ways of solving it. :D