如何在VB.NET中存储组合框的输出?

时间:2014-11-11 12:56:10

标签: vb.net forms

我正在尝试创建我的代码,以便从ComboBox获取输出然后将其存储到变量“Planet”,我将如何进行此操作?我试过Planet = ComboBox1,但这不起作用。


Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim Planet As String

    Planet = ComboBox1
    If Planet =


    End If

End Sub

我现在用过:

Planet = Convert.ToString(ComboBox1)

但是我得到了输出“System.Windows.Forms.ComboBox,Items.Count:8”,我在这个ComboBox中有8个字符串,它似乎正在输出。当我在ComboBox中选择一个项目时,我从下拉列表中点击其中一个行星,这是我需要重新调整的。

2 个答案:

答案 0 :(得分:1)

这是可能有用的鳕鱼:

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object,e As System.EventArgs)处理ComboBox1.SelectedIndexChanged     Dim Planet As String

Planet = ComboBox1.SelectedValue.Tostring()
If Planet = "ConditionalValue" Then
'Your Code if True
ELSE
'YOUR CODE IF FALSE

End If

End Sub

答案 1 :(得分:1)

很抱歉在SelectedText是错误的属性之前没有调试它,你应该只使用它来检索用户在组合框的文本框部分中选择的文本。您确实在更改索引后选择了所有文本,但直到此事件运行后才会发生。请改用SelectedItem.ToString()。这是代码:

' I have added a combobox and a lable and the code is written on SelectedIndexChanged Event

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If ComboBox1.SelectedItem.ToString = "Text1" Then
        Label1.Text = "Text1"
    End If
    If ComboBox1.SelectedItem.ToString = "Text2" Then
        Label1.Text = "Text2"
    End If
End Sub