Search code examples
wpfvb.netcombobox

How to get combobox text after user enters a value


I have a wpf combobox bound to a CollectionView. It is set so the user can edit the entry in the combobox selectionbox. When the user types a value NOT in the list, I want to grab that value to use elsewhere but I cannot figure out how to get the text entered by the user. Online help suggested using the Text property but there is no such property. I'm using VS2013, Framework 4.5 and VB.


Solution

  • But there exists a Text property (MSDN) for the combobox. Also, to catch the event of the user editing the ComboBox, you should subscribe to the TextChanged event. I created a simple WPF with a ComboBox and added the event handler.

    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" IsEditable="True" TextBoxBase.TextChanged="comboBox_TextChanged" />
    

    And the function that handles the TextChanged:

    Private Sub comboBox_TextChanged(sender As Object, e As RoutedEventArgs)
        MessageBox.Show("Text changed to: " + comboBox.Text)
    End Sub
    

    a simple wpf example