ComboBox Control WPF In XAML using VB.NET

Here we see how to create and use combobox control in XAML.
  • 3047
 
Here we see how to create and use ComboBox control in XAML. To do that we create a TextBox, Button and ComboBox control in the XAML. we select the items from the ComboBox and click on the button the selected items will be display on the TextBox.  

ComboBox Control

The ComboBox control is used to select only one item from many items. Use Items collection property to add the items.

Creating ComboBox control in XAML

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

 

The Width and Height property represents the width and the height of the control and Name property represents name of the control.

 

f1.gif
 

Figure1.gif

 

Important Properties

These are the some important property of the ComboBox control.

Items - The collection of items in the list.

Content - Programmatic name of the control.

ForeColor - Color of the text within the control.

For example

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

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.Object, ByVal e As System.Windows.RoutedEventArgs) Handles 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.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        TextBox1.Text = ComboBox1.SelectedItem

    End Sub

Now run the application

f2.gif

Figure2.gif

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

f3.gif

Figure3.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.