【问题标题】:vb.net, combobox.datasource will change selected index?vb.net,combobox.datasource 会改变选定的索引吗?
【发布时间】:2012-03-01 19:40:36
【问题描述】:

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

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

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

所以我将 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 事件。

所以上面代码的结果是目标 1 实现了,但目标 2 永远没有实现。 combobox1.selected 索引始终为 0,combobox2.selected 索引也始终为 0。

【问题讨论】:

    标签: vb.net winforms combobox


    【解决方案1】:

    这里有 2 个代表国家和大陆的类:

    '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
    

    【讨论】:

      猜你喜欢
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      相关资源
      最近更新 更多