您好我有名字的组合框(" Combobox1"," Combobox1a"," Combobox2"," Combobox2a",&#34 ; combobox3"" Combobox3a")。当combobox1中的selectedindex发生变化时,combobox1a将添加4个项目。与combobox2和2a,combobox3和3a相同。当我这样做时,我必须在combobox1,combobox2和combobox3中编码。所以生病了尝试制作一个方法,这样当组合框中的selectedindexchange会调用这个方法时。
继承我的代码。
Dim cmbName as String = ""
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbName = "1"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbName = "2"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbName = "3"
cmbSelectedIndexChanged()
End Sub
Private Sub cmbSelectedIndexChanged()
"combobox" & cmbName.Items.add("data1")
"combobox" & cmbName.Items.add("data2")
"combobox" & cmbName.Items.add("data3")
"combobox" & cmbName.Items.add("data4")
End Sub
我只是想让我的代码更小,这样每次我的组合框索引更改它只会调用该方法。
答案 0 :(得分:0)
您可以在控件集合中找到其他组合,如下所示
combo = CType(Me.Controls.Find("combobox1", True)(0), ComboBox)
答案 1 :(得分:0)
你的意思是这样的吗?
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbSelectedIndexChanged("1")
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbSelectedIndexChanged("2")
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbSelectedIndexChanged("3")
End Sub
Private Sub cmbSelectedIndexChanged(cmbName as string)
Dim Combo as ComboBox = CType(Me.Controls.Find("combobox" & cmbName), ComboBox)
Combo.Items.add("data1")
Combo.Items.add("data2")
Combo.Items.add("data3")
Combo.Items.add("data4")
End Sub