根据另一个组合框选定值设置组合框值 - VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25184896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:59:48  来源:igfitidea点击:

Set Combobox Value base on another Combobox Selected Value - VB.NET

vb.netcombobox

提问by AznDevil92

I have two comboboxes. If the user selects a certain value from the first combobox then I want the second combobox to automatically change and select to its corresponding value.

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

For example:

例如:

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

I have also tried:

我也试过:

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

Can anyone help?

任何人都可以帮忙吗?

回答by pkatsourakis

//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 

回答by Chase Ernst

You will need to use the event handler for the comboBoxthat you are checking. Inside the event handler you can use a slight variation of your if statement:

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

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

When using the SelectedIndexfunction, the counting starts at the 0th position.

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

When Using SelectedValueor SelectedItemis used for getting that text or value inside the combobox.

当使用SelectedValueSelectedItem用于获取combobox.

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

or

或者

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