VB.NET ComboBox结果选中的项目

时间:2013-06-16 09:02:01

标签: vb.net combobox selection

您好我有一个vb Windows窗体应用程序,它有一个来自form1的ComboBox我有一些代码读取一些注册表并将项目结果添加到组合框。我想选择其中一个结果并运行一个启动过程。我的问题是,在选择项目时我将代码放在哪里然后执行某些操作以及如何确定已选择的内容?

我的代码来查询注册表项

 Dim Key, Reader As RegistryKey, Y As String
    Key = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts", False)
    For Each X In Key.GetSubKeyNames
        Reader = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts\" & X, False)
        If Reader.GetValueNames().Contains("AppTitle") Then
            Y = Reader.GetValue("AppTitle")

            If Not ComboBox1.Items.Contains(Y) Then ComboBox1.Items.Add(Y)
        End If

如果我这样做,它只显示一个空白的消息框,我还没有从组合框中选择该文本。

If ComboBox1.SelectedText Then
            MessageBox.Show(ComboBox1.SelectedText())
        End If

2 个答案:

答案 0 :(得分:1)

您订阅了SelectedIndexChanged事件,编写了这样的方法

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

    Dim comboBox As comboBox = CType(sender, comboBox)

    ' Caution, the event could be called also when there is nothing selected
    if combBox.SelectedItem IsNot Nothing Then
         Dim curValue = CType(combBox.SelectedItem, String)
         'do your stuff with the selected key' 
    End If
End Sub 

答案 1 :(得分:0)

if combBox.SelectedItem IsNot Nothing Then

    Dim cmbselected As String = DirectCast(DirectCast(DirectCast(DirectCast(combBox, System.Windows.Controls.ComboBox).SelectedValue, System.Object), System.Data.DataRowView).Row, System.Data.DataRow).ItemArray(0)

End If