gpt4 book ai didi

vb.net - 一个 ComboBox 的项目如何由另一个确定?

转载 作者:行者123 更新时间:2023-12-04 05:56:40 25 4
gpt4 key购买 nike

我正在使用 Visual Basic 2010,我想知道的是如何在单个 ComboBox 上创建不同的列表...
实际上我有 2 个 ComboBox ......第一个里面有一个项目列表......我想根据第一个 ComboBox 的选择创建一个具有不同类型列表的第二个 ComboBox ......

例如:所有大洲的第一个组合框和第二个组合框对于所有国家,我希望第二个组合框的国家列表根据从第一个组合框列表中选择的国家/地区而变化...

最佳答案

这是代表国家和大陆的两个类:

'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到一个名为 cmbContinent 和 cmbCountry 的表单,然后将以下代码添加到您的表单中:
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

关于vb.net - 一个 ComboBox 的项目如何由另一个确定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9442833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com