1

嗨,我有一个 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
4

2 回答 2

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 
于 2013-06-16T09:12:53.613 回答
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
于 2014-03-09T17:32:53.913 回答