Results 1 to 5 of 5

Thread: Comboboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Comboboxes

    I'm doing a piece of work for college and I wanted to know how to make an item in one combobox become invisible when one item is selected in another combobox. My scenario is that I have a combobox for hot drinks (e.g tea, coffee and hot chocolate) and I have another combobox for the prices, and for example when I select tea I would like the other prices to disappear and for it to only show the price for the tea. Does anybody know how to do this? Any suggestions would be appreciated!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Comboboxes

    Try this:
    Code:
    Public Class Form1
        'Controls
        Private comboProducts, comboPrice As ComboBox
    
        'Objects
        Private priceGuide As Dictionary(Of String, Decimal)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Create the dictionary and add coffee($3), tea($1), and hot chocolate($0.50)
            priceGuide = New Dictionary(Of String, Decimal) From { _
                {"Coffee", 3D}, _
                {"Tea", 1D}, _
                {"Hot Chocolate", 0.5D}}
    
            'Create the comboboxes
            comboProducts = New ComboBox With {.Location = New Point(5, 5), .Width = 100}
            comboPrice = New ComboBox With {.Left = comboProducts.Right + 5, .Top = comboProducts.Top, .Width = comboProducts.Width}
    
            'Add the keys of the dictionary to the comboProducts
            comboProducts.Items.AddRange(priceGuide.Keys.ToArray)
    
            'Add the controls to the form
            Me.Controls.AddRange({comboProducts, comboPrice})
    
            'Create the SelectedIndexChanged event handler for the comboProducts
            AddHandler comboProducts.SelectedIndexChanged, AddressOf comboProducts_SelectedIndexChanged
        End Sub
    
        Private Sub comboProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            'Check if the user selected an item
            If comboProducts.SelectedIndex > -1 Then
                'Get the value and call it's ToString method converting it to currency
                Dim selectedValue As String = priceGuide(comboProducts.Text).ToString("c")
    
                'Clear any existing items
                comboPrice.Items.Clear()
                'Add the selectedValue
                comboPrice.Items.Add(selectedValue)
                'Select the new item
                comboPrice.SelectedIndex = 0
            End If
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Comboboxes

    Certainly dday9 will work fine but there's another way which uses data binding and a relation. This is a really nice example posted in the code bank by jmc. http://www.vbforums.com/showthread.p...ms)&highlight=

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Re: Comboboxes

    Thanks so much for the replies! I will give that code a go in a bit and tell you how it goes!

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Re: Comboboxes

    It works perfectly, that you so much! I'm sure you'll be seeing more of me on here haha

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