问题描述
让我尝试用最简单的方式描述我的问题:我有combobox1和combobox2。我希望实现两件事:
Let me try to describe my problems in the simplest way: I have combobox1 and combobox2. I hope to achieve two things:
-
Combox1绑定到list1(字符串列表)。当用户选择list1中的项目时,将从数据库中获取list2(字符串列表),并将combobox绑定到list2。
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.
在combobox1中的combobox1和text2中,这两个值将显示在组合框中。因此我将DropDown设置为dropdpwnstyle到两个组合框。
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:
-
After Me.combobox1.SelectedText = text1, Me.combobox1.SelectedText =。但Me.combobox1.Text = text1。这是因为combobox1.SelectedIndex = -1?
After Me.combobox1.SelectedText=text1, actually, Me.combobox1.SelectedText="". But Me.combobox1.Text=text1. Is this because combobox1.SelectedIndex=-1?
Combobox1.datasource = m_list1将combobox1.selectedindex从-1更改为0.这将触发combobox.selectedIndexchange
Combobox1.datasource=m_list1 changes combobox1.selectedindex from -1 to 0. This will fire the combobox.selectedIndexchange event.
因此,上述代码的结果是实现了目标1,但目标2从未实现。
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.
推荐答案
这里有两个代表国家和大陆的类:
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
现在将2个组合框添加到名为cmbContinent和cmbCountry的表单中,然后将以下代码添加到您的表单:
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
这篇关于vb.net,combobox.datasource会改变所选的索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!