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



我正试图制作我的代码,以便从组合框中获得输出,然后将其存储在变量"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个字符串,似乎这就是它输出的内容。当我在组合框中选择一个项目时,我会从下拉列表中单击其中一个行星,这是我需要重新运行的。

很抱歉在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

以下是可能有所帮助的代码:

Private Sub ComboBox1_SelectedIndexChanged(发送方为System.Object,e为System.EventArgs)处理ComboBox1.SelectedIndexChange将行星变暗为字符串

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

结束子

最新更新