Valerio
Valerio

Reputation: 278

How can one ComboBox's items be determined by another?

I'm Using Visual Basic 2010, and what i want know is how i can create different list on a single ComboBox... practically i've 2 ComboBox... the first with a list of Items inside.. and i want to create a second ComboBox With Different types of list based on the choice of the first ComboBox..

Ex: First Combobox with all the continents and the secondo ComboBox With all the Nations, i want that the Nations list of the Second ComboBox change according to the Country selected from the list of the first ComboBox...

Upvotes: 2

Views: 3347

Answers (1)

Amen Ayach
Amen Ayach

Reputation: 4348

Here are two classes that represents country and continent :

'Coded by Amen Ayach's DataClassBuilder @25/02/2012
Public Class CountryCls

    Private _CountryID As Integer
    Public Property CountryID() As Integer
        Get 
            Return _CountryID
        End Get
        Set(ByVal value As Integer)
            _CountryID = value
        End Set
    End Property

    Private _CountryName As String
    Public Property CountryName() As String
        Get 
            Return _CountryName
        End Get
        Set(ByVal value As String)
            _CountryName = value
        End Set
    End Property

    Private _ContinentID As Integer
    Public Property ContinentID() As Integer
        Get 
            Return _ContinentID
        End Get
        Set(ByVal value As Integer)
            _ContinentID = value
        End Set
    End Property

End Class


'Coded by Amen Ayach's DataClassBuilder @25/02/2012
Public Class ContinentCls

    Private _ContinentID As Integer
    Public Property ContinentID() As Integer
        Get 
            Return _ContinentID
        End Get
        Set(ByVal value As Integer)
            _ContinentID = value
        End Set
    End Property

    Private _ContinentName As String
    Public Property ContinentName() As String
        Get 
            Return _ContinentName
        End Get
        Set(ByVal value As String)
            _ContinentName = value
        End Set
    End Property

End Class

Now add two ComboBoxs to a form named cmbContinent and cmbCountry, then add the following code to your form :

Dim ContinentList As New List(Of ContinentCls)
Dim CountryList As New List(Of CountryCls)

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

    'Initialize some fake data
    For i = 1 To 3
        ContinentList.Add(New ContinentCls With {.ContinentID = i, .ContinentName = "Continent" + CStr(i)})
        For j = 1 To 5
            CountryList.Add(New CountryCls With {.ContinentID = i, .CountryID = j, .CountryName = "Cont" + CStr(i) + " - Country" + CStr(j)})
        Next
    Next

    'Filling out ContinentCombo
    With cmbContinent
        .ValueMember = "ContinentID"
        .DisplayMember = "ContinentName"
        .DataSource = ContinentList
    End With

End Sub

Private Sub cmbContinent_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbContinent.SelectedValueChanged
    Try
        'Filling out CountryCombo according to seleced ContinentCombo
        With cmbCountry
            .ValueMember = "CountryID"
            .DisplayMember = "CountryName"
            .DataSource = CountryList.Where(Function(f) f.ContinentID = cmbContinent.SelectedValue).ToList
        End With
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Upvotes: 3

Related Questions