vb.net,combobox.datasource会改变选定的索引吗?

时间:2012-03-01 19:40:37

标签: vb.net winforms combobox

让我试着用最简单的方式描述我的问题:我有combobox1和combobox2。我希望实现两件事:

  1. Combox1绑定到list1(字符串列表)。当用户选择list1中的项目时,将从数据库中获取list2(字符串列表),并将组合框绑定到list2。

  2. 如果用户在combobox1中指定text1,在combobox2中指定text2,则无论绑定列表如何,这两个值都将显示在组合框中。

  3. 所以我将DropDown设置为dropdpwnstyle到两个组合框。

    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
    

    当我调试时,我注意到两件事:

    1. 在Me.combobox1.SelectedText = text1之后,实际上,Me.combobox1.SelectedText =“”。但是Me.combobox1.Text = text1。这是因为combobox1.SelectedIndex = -1?

    2. Combobox1.datasource = m_list1将combobox1.selectedindex从-1更改为0.这将触发combobox.selectedIndexchange事件。

    3. 因此上述代码的结果是实现了目标1,但目标2永远不会实现。 combobox1.selected索引始终为0,而​​combobox2.selected索引也始终为0。

1 个答案:

答案 0 :(得分:1)

这里有两个代表国家和大陆的课程:

'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的表单中,然后将以下代码添加到表单中:

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