Silverlight ListBox Control in VB.Net

In this article, we will learn the use of ListBox control.
  • 2121

Introduction: ListBox control is used for display the items in the list. It is similar to ComboBox - except that it shows all items in flat list.

For working with ListBox, We take a silverlight application and then a ListBox from Toolbox or by writing the following XAML code in editor

<Grid x:Name="LayoutRoot" Background="White">
    <ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1"

VerticalAlignment="Top" Width="120" />

We can add items to ListBox by it's Items property or by writing the following XAML code

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1"

VerticalAlignment="Top" Width="120">
            <ListBoxItem Content="Cricket" />
            <ListBoxItem Content="Hockey" />
            <ListBoxItem Content="Football" />
            <ListBoxItem Content="Tennis" />

When we run our application, it will look like below figure


Figure 1

 We can change the display style of ListBox by it's properties. Some are below

  • Background: To set background color.

  • BorderBrush: To set border color.

  • BorderThickness: To set thickness of ListBox border.

Now ListBox will look different. Like Below figure


Figure 2

There are some properties related to font of ListBox, some are below

  • FontFamily: To select font of ListBox items.

  • FontSize: To select font size.

  • FontStretch: To stretch the font.

  • FontStyle: To set style of font as Normal or Italic.

  • Fontweight: To set thickness of font.

  • Foregruond: To set font color.

I made some changes using above properties. Now ListBox is looking as figure 3


Figure 3

Suppose, we want to get selected item of ListBox into TextBox. We take a textbox and write the following code on SelectionChanged event of ListBox .

Private Sub ListBox1_SelectionChanged(sender As System.Object, e AsSystem.Windows.Controls.SelectionChangedEventArgs) Handles ListBox1.SelectionChanged
        Dim str As ListBoxItem = TryCast(ListBox1.SelectedItem, ListBoxItem)
        TextBox1.Text = str.Content.ToString()

When we run our application and select the item, the selected item will be displayed in textbox. Like following figure


Figure 4

Conclusion: In this article, we learn about ListBox Control, about it's properties and it's use in our application.  


More Articles

© 2019 DotNetHeaven. All rights reserved.