How to populate a second drop down based on the content of a first

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have two drop downs and the content of the second one must changed everytime the user changes the selection in the first one. Is any easy way of doing this?

Thanks in advance.
Alonso
 
Are these bound to tables in a dataset. If so, bind one to the master and
one to a DataRelation which you need to create between the master and
dependent table.

If this is not the case that they are bound, then what criteria are you
using to fill the Combo's

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

Alonso said:
Hi,

I have two drop downs and the content of the second one must changed
everytime the user changes the selection in the first one. Is any easy way
of doing this?
 
Hi,

Change the content of the second combobox it the first combobox
selected index changed event.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList

ComboBox1.Items.Add("Fruit")
ComboBox1.Items.Add("Pets")
ComboBox1.Items.Add("Cars")
ComboBox1.SelectedIndex = 0
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

ComboBox2.Items.Clear()
Select Case ComboBox1.SelectedIndex
Case 0
ComboBox2.Items.Add("Apple")
ComboBox2.Items.Add("Peach")
ComboBox2.Items.Add("Orange")
Case 1
ComboBox2.Items.Add("Dog")
ComboBox2.Items.Add("Cat")
ComboBox2.Items.Add("Parrot")
Case Else
ComboBox2.Items.Add("Pickup truck")
ComboBox2.Items.Add("SUV")
ComboBox2.Items.Add("Compact Car")
End Select
ComboBox2.SelectedIndex = 0
End Sub


Ken
--------------
 
* =?Utf-8?B?QWxvbnNv?= said:
I have two drop downs and the content of the second one must changed
everytime the user changes the selection in the first one. Is any easy
way of doing this?

You could store objects in the 1st combobox that provide the strings for
the 2nd dropdown in a public property:

\\\
Public Class MyItem
Private m_Text As String
Private m_SubItems() As String

Public Property Text() As String
Get
Return m_Text
End Get
Set(ByVal Value As String)
m_Text = Value
End Set
End Property

Public Property SubItems() As String()
Get
Return m_SubItems()
End Get
Set(ByVal Value() As String)
m_SubItems = Value
End Set
End Property

Public Overrides Function ToString() As String
Return m_Text
End Function
End Class
///

When selection in the 1st combobox changes, you can take the selected
item of the 1st combobox and loop through its 'SubItems' and add the
items to the 2nd combobox.
 
Back
Top