CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    5

    Help with combo boxes and cost

    Im new to VB.net and doing a project for college work.

    Basically, I have combo boxes that have different computer components.

    Here is part of the code for you to understand:

    Code:


    With cboCPU
    .Items.Add("Intel Core i7-920 Bloomfield 2.66GHz Quad core")
    .Items.Add("Intel Core 2 Duo E8400 Wolfdale 3.0GHz Dual Core")
    .Items.Add("Intel Core 2 Quad Q8200 2.33GHz Quad Core")
    .Items.Add("AMD Phenom II X4 965 Black Edition Deneb 3.4GHz Quad core")
    End With



    The problem im faced with is how to assign a cost to each of these so that i am able to calculate the total. If the user selects the first item display the cost in sub total1 for example.
    I looked on google and found out about multi columns in a combo box but the other problem is I cant just add code I dont understand because I have to explain what each part of the code is doing.

    Help would be much appreciated because im faced with brick wall at the moment.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Help with combo boxes and cost

    Well, ordinarily, you'd make use of some sort of array, to store the prices. An array is a group of variables with the same datatype, that share the same name. For example :

    Code:
    Dim PC_Cost(3) As Single 'Creates 4 array elements
    
    PC_Cost(0) = 200.45 'Assigns a value to first item in array
    PC_Cost(1) = 400.25 'Assigns a value to second item in array
    PC_Cost(2) = 100.15 'Assigns a value to third item in array
    PC_Cost(3) = 50.50 'Assigns a value to fourth item in array
    Why 4 items, and why 0 to 4 instead of 1 to 4 ¿

    4 Items because you have 4 items in your combobox
    It starts at zero, beacuse all arays are 0 based, and the first index is always 0

    Now, how do I link these to the combobox ¿

    You simply double click the combobox, that will open the SelectedIndexChanged event handler, and type code similar to the following :
    Code:
     Select Case ComboBox1.SelectedIndex
                Case 0
    'Do whatever calculations here
    'This Item ( Intel Core i7-920 Bloomfield 2.66GHz Quad core ) is now linked to the first item in the PC_Cost array, which is 200.45 
    End Select
    And so on.

    See, it is not so difficult

  3. #3
    Join Date
    Jan 2010
    Posts
    5

    Re: Help with combo boxes and cost

    Ah right that makes sense to me. Ill go and experiment with this.
    I have only just started to learn vb so when i google something to try and find out roughly how to do something, majority of the time its loads of code I havnt even come across before so i have no idea where to even begin trying to decode it all.

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

Featured