简体   繁体   中英

VB.NET ComboBox results selected item

Hi I have a vb windows form application that has a ComboBox from the form1 I have some code that reads some registry and adds item results to combobox. I would like to select one of the results and run a start process. My problem is where do I put the code when item is selected then do something and how to I determine what has been selected?

My Code to query registry keys

 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 i do somehting like this, it just shows a blank messagebox and I have not selected that text from combobox yet.

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

You subscribe to the SelectedIndexChanged event writing a method like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM