根据另一个Combobox Selected Value - VB.NET设置Combobox Value

时间:2014-08-07 14:17:04

标签: vb.net combobox

我有两个组合框。如果用户从第一个组合框中选择某个值,那么我希望第二个组合框自动改变并选择其相应的值。

例如:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedValue = 3 Then 
     Me.WeightComboBox1.SelectedValue = 2

我也尝试过:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedText = "Internal Guidelines" Then                 
    Me.WeightComboBox1.SelectedText = "None"

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

//selected index changed event for the combo box 

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles InstitutionAdvisoryFirmsComboBox1.SelectedIndexChanged

    If Me.InstitutionAdvisoryFirmsComboBox1.SelectedIndex = 3 Then 
        If Me.WeightComboBox1.Items.Count >= 2 Then // make sure you are not accessing an index out of range
            Me.WeightComboBox1.SelectedIndex = 2
        End If
    End If

End Sub 

答案 1 :(得分:2)

您需要使用您正在检查的comboBox的事件处理程序。在事件处理程序中,您可以使用if语句的略微变体:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedIndex = 1 Then                 
    Me.WeightComboBox1.SelectedIndex = 2

使用SelectedIndex功能时,计数从第0位开始。

使用SelectedValueSelectedItem用于在combobox内获取该文字或值。

Dim ComboValue As String = comboBox1.SelectedValue.ToString();

Dim ComboValue As String = comboBox1.SelectedItem.ToString();