Let me try to describe my problems in the simplest way: I have combobox1 and combobox2. I hope to achieve two things:
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.
If user specifies text1 in combobox1 and text2 in combobox2, then these two values will be shown in the comboboxes regardless of the bound lists.
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, actually, Me.combobox1.SelectedText="". But Me.combobox1.Text=text1. Is this because combobox1.SelectedIndex=-1?
Combobox1.datasource=m_list1 changes combobox1.selectedindex from -1 to 0. This will fire the combobox.selectedIndexchange event.
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.