Results 1 to 9 of 9

Thread: [RESOLVED] add item to combo box and related price

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    Resolved [RESOLVED] add item to combo box and related price

    Hi again guys.

    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.
    Last edited by pukisoft; Mar 27th, 2007 at 04:23 PM.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: add item to combo box and related price

    You can use the Itemdata property of thre combo to store the price.

    vb Code:
    1. Combo1.AddItem "A Service"
    2. Combo1.ItemData(Combo1.NewIndex) = "1.98"
    3. Combo1.AddItem "Another Service"
    4. Combo1.ItemData(Combo1.NewIndex) = "5.67"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    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

  5. #5
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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:
    1. Dim strPrice() As Currency
    2.  
    3. ReDim strPrice(0)
    4. Combo1.AddItem "A Service"
    5. strPrice(0) = 1.98
    6.  
    7. Combo1.AddItem "Another Service"
    8. ReDim Preserve strPrice(UBound(strPrice) + 1)
    9. strPrice(UBound(strPrice)) = 5.67
    10.  
    11. Label1.Caption = strPrice(0)

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    Re: [RESOLVED] add item to combo box and related price

    Thnkx guys. now I have two ways of solving it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width