ARTICLE

How to get currently selected ComboBox item in WPF using VB.NET

Posted by Dea Saddler Articles | WPF using VB.NET May 20, 2011
In this article you will learn that how you can get currently selected comboBox item on Button click
Download Files:
 
Reader Level:

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 As SelectionChangedEventArgs)
            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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor