Silverlight ComboBox control in VB.NET

In this article we will learn how to use ComboBox control in Silverlight.
  • 2603

In this article we will learn how to use ComboBox control in Silverlight.

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.Object, ByVal 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:

<UserControl x:Class="SilverlightApplication10.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="320" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

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

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

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

    </Grid>

</UserControl>

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

Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles MyBase.Loaded

        If Not IsPostBack Then

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

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

            Next

        End If

 

    End Sub

Use Page.IsPostBack property to check the post backs.

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

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles Button1.Click

        TextBox1.Text = ComboBox1.SelectedItem

    End Sub

 

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

cb3.gif

Figure 3.

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

cb4.gif

Figure 4.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.