-
Dec 10th, 2014, 11:50 AM
#1
Thread Starter
New Member
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!
-
Dec 10th, 2014, 12:21 PM
#2
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
-
Dec 10th, 2014, 01:01 PM
#3
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=
-
Dec 11th, 2014, 08:00 AM
#4
Thread Starter
New Member
Re: Comboboxes
Thanks so much for the replies! I will give that code a go in a bit and tell you how it goes!
-
Dec 11th, 2014, 08:13 AM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|