WPF Currently selected ComboBox item in VB.NET

In this article you will learn that how you can get currently selected comboBox item on Button click.
  • 6064
Download Files:
 

Introduction
 
Here in this article I am explaining that how you can get currently selected items in a comboBox on button click. The implementation needs one ComboBox and a Button control. Then you add items in ComboBox , a little code and when you click on Button a Message will display with the Currently selected item of the ComboBox. 

Getting Started

  • Simply create a new WPF application. 
  • Drag a ComboBox and a Button Control on Main Window. Your window will look like below.

    wpfcombo1.gif
     
  • Your MainWindow.xaml page will look like below.

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width
    ="277">
        <StackPanel>
            <ComboBox Name="comboBox" IsEditable="True" Margin="5"
    SelectionChanged="ComboBox_SelectionChanged">
                <ComboBoxItem Content="ComboBox Item 1" Selected="ComboBoxItem_Selected" />
                <ComboBoxItem Content="ComboBox Item 2" Selected="ComboBoxItem_Selected" />
                <ComboBoxItem Content="ComboBox Item 3" Selected="ComboBoxItem_Selected"IsSelected="True"/>
                <ComboBoxItem Content="ComboBox Item 4" Selected="ComboBoxItem_Selected" />
                <ComboBoxItem Content="ComboBox Item 5" Selected="ComboBoxItem_Selected" />
            </ComboBox>
            <Button Content="Show Selected Item" Margin="5" Width="100" Click="Button_Click"Name="btnSelect"
    />
        </StackPanel>
    </
    Window>
     
  • Then add the below code in code behind file.

       Private Sub btnSelect_Click(sender As Object, e As RoutedEventArgs)
            Dim listitem As ComboBoxItem = TryCast(comboBox.SelectedItem, ComboBoxItem)
            If listitem IsNot Nothing Then
                MessageBox.Show("present Current items in list: " &Convert.ToString(listitem.Content),
                Title)
            ElseIf Not [String].IsNullOrEmpty(comboBox.Text) Then
                MessageBox.Show("Enterde Text: " + comboBox.Text, Title)
            End If
        End Sub
        Private Sub ComboBox_SelectionChanged(sender As Object, e AsSelectionChangedEventArgs)
            If Not IsInitialized Then
                Return
            End If
             Dim listitem As ComboBoxItem = TryCast(comboBox.SelectedItem, ComboBoxItem)
            If listitem IsNot Nothing Then
                MessageBox.Show("Selected item: " & Convert.ToString(listitem.Content), Title)       End If
        End Sub
        Private Sub ComboBoxItem_Selected(sender As Object, e As RoutedEventArgs)
            If Not IsInitialized Then
                Return
            End If
            Dim listitem As ComboBoxItem = TryCast(e.OriginalSource, ComboBoxItem)
            If listitem IsNot Nothing Then
                MessageBox.Show(Convert.ToString(listitem.Content) & " was selected.", Title)
            End If
        End 
    Sub
     
  • Now run your application.
Output:-

wpfcombo2.gif

wpfcombo3.gif

wpfcombo4.gif

Summary
 
In this article you learned that how you can get currently selected item of ComboBox on Button Click. 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.