2012-02-25 92 views
2

我使用的是Visual Basic 2010,我想知道的是我如何在一個ComboBox上創建不同的列表... 幾乎我已經2 ComboBox ...第一個列表裏面的項目..和我想根據第一個組合框的選擇創建第二個組合框與不同類型的列表。一個ComboBox的項目如何由另一個確定?

例如:第一個Combobox與所有大洲和第二個組合框與所有國家,我希望國家列表根據從第一組合框的列表中選擇該國第二組合框的變化......

+0

這是一個vb.net問題。我已經爲你重製了它。 – 2012-02-25 10:15:11

回答

3

這裏是代表國家大陸兩類

'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 

現在添加兩個ComboBoxs到一個名爲cmbContinentcmbCountry形式,然後將下面的代碼添加到您的窗體:

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 
+0

謝謝,巴勃羅你有我的投票。 – 2012-02-26 16:16:08

相關問題