Zen
Zen

Reputation: 29

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

Upvotes: 1

Views: 25766

Answers (2)

Milan Sheth
Milan Sheth

Reputation: 962

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

Upvotes: 0

Steve
Steve

Reputation: 216243

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 

Upvotes: 1

Related Questions