ComboBox control in WPF using VB.NET

In this article we will learn how to use ComboBox control in WPF using VB.NET.
  • 4925

In this article we will learn how to use ComboBox control in WPF using VB.NET.

ComboBox control

ComboBox allows to select only one item from many items. Use Items collection to add the items.

Properties: These are the following properties.

cb1.gif
 

Figure 1.

Items - The collection of items in the list.

Content - Programmatic name of the control.

ForeColor: Color of the text within the control.

ComboBox event

Private Sub ComboBox1_SelectionChanged(ByVal sender As System.ObjectByVal e AsSystem.Windows.Controls.SelectionChangedEventArgs
Handles ComboBox1.SelectionChanged

End Sub

For example

Drag a ComboBox, Button and TextBox control on the form. when we select the items in the Combobox and click on the button its should be display on the Textbox control.

The form looks like this.

cb2.gif
 

Figure 2.

XAML code:

<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="350" Width="525">

    <Grid>

        <ComboBox Height="23"  HorizontalAlignment="Left" Margin="10,10,0,0"Name="ComboBox1" VerticalAlignment="Top" Width="120" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="180,12,0,0" Name="TextBox1"VerticalAlignment="Top" Width="120" />

        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="117,72,0,0"Name="Button1" VerticalAlignment="Top" Width="75" />

    </Grid>

</Window>

Now double click on the form and add the below code.

Private Sub Window_Loaded(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles MyBase.Loaded

        For i As Integer = 1950 To DateTime.Now.Year

            ComboBox1.Items.Add(i.ToString())

        Next

    End Sub

Now double click on the button control and add the following code.

Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button1.Click

        TextBox1.Text = ComboBox1.SelectedItem

    End Sub

 

Now setting the property BackgroundForeground and BorderBrush properties of CheckBox control.


 

cb5.gif
 

Figure 3.

The form looks like this.


 

cb6.gif 

Figure 4.

 

XAML code

<ComboBox Height="23"  HorizontalAlignment="Left" Margin="10,10,0,0" Name="ComboBox1"VerticalAlignment="Top"
 Width="120" Background="Blue" Foreground="DarkRed"BorderBrush="DarkGreen" />

Now save and run the application And click the ComboBox than DropDownList items will be display.

cb3.gif
 

Figure 5.

Now select the items from the ComboBox then click on the button the selected items will be display on the TextBox.

cb4.gif
 

Figure 6.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.