2012-02-25 6 views
2

私はVisual Basic 2010を使用しています。私が知りたいことは、1つのコンボボックスで異なるリストを作成する方法です。 実際には2つのComboBoxを持っています。私は最初のComboBoxの選択に基づいてリストの種類が異なる2番目のComboBoxを作成したいと思います。1つのComboBoxのアイテムを別のアイテムでどのように決定できますか?

例:すべての大陸と2番目のComboBoxを持つ最初のComboboxすべての国と、第二にコンボボックスの最初のコンボボックスのリストから選択された国に応じて変更...ここ

+0

これはvb.net質問です。私はあなたのためにそれを記録した。 –

答えて

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 

今、あなたのフォームに次のコードを追加し、cmbContinentcmbCountryという名前のフォームに2つのComboBoxsを追加します。

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

ありがとう、パブロ私の投票を持っています。 –

関連する問題