1
votes

Let me try to describe my problems in the simplest way: I have combobox1 and combobox2. I hope to achieve two things:

  1. Combox1 is bound to list1 (a list of string). When a user selects an item in list1, list2 (a list of string) will be obtained from database and combobox is bound to list2.

  2. If user specifies text1 in combobox1 and text2 in combobox2, then these two values will be shown in the comboboxes regardless of the bound lists.

So I set DropDown as dropdpwnstyle to both comboboxes.

Public Sub New(Optional ByVal text1 As String = "", Optional ByVal text2 As String = "")
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
      Me.combobox1.selectedText=text1   
      Me.combobox2.selectedText=text2
End Sub


Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    BindComboBox1()
End Sub

Private Sub BindComboBox1()
      'm_list1 is a list of string 

combobox1.DataSource = m_list1

End Sub

Private Sub GetCombobox2()

    'based on the selected item in combobox1, m_list2 which is a list of string is obtained

    ComboBox2.DataSource = m_list2
End Sub

Private Sub combobox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles combobox1.SelectedIndexChanged
        If combobox1.SelectedIndex <> -1 Then
            GetCombobox2()

        End If
End Sub

When I debug, I notice two things:

  1. After Me.combobox1.SelectedText=text1, actually, Me.combobox1.SelectedText="". But Me.combobox1.Text=text1. Is this because combobox1.SelectedIndex=-1?

  2. Combobox1.datasource=m_list1 changes combobox1.selectedindex from -1 to 0. This will fire the combobox.selectedIndexchange event.

So the results of above code is that goal 1 is achieved, but goal 2 is never achieved. combobox1.selected index is always 0 and combobox2.selected index is always 0 too.

1

1 Answers

1
votes

Here 2 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 2 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