Select and Unselect all items in WPF ListBox using VB.NET

Here we will see that How to select and unselect all items in WPF ListBox.
  • 5657

Here we will see that How to select and unselect all items in WPF ListBox using VB.NET.

ListBox control

The WPF ListBox control Contains a list of items. The ListBox control supports single selection and multiple selection of the Items from a list of items. In this example we take a ListBox control and add some item in the ListBox using item property of the ListBox control.

Items - Items collection property is used to add the items in the ListBox control.

XAML code

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120" SelectionMode="Multiple">

            <ListBoxItem Content="Red" />

            <ListBoxItem Content="Green" Name="ListBoxItem2" />

            <ListBoxItem Content="Blue" Name="ListBoxItem1" />

            <ListBoxItem Content="Pink" />

            <ListBoxItem Content="Black" />

        </ListBox>       

For example

Drag and drop one ListBox control and two Button control which has the name select all and unselect all.

The window form looks like this.

l1.gif

Figure1.gif

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>        

        <ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120" SelectionMode="Multiple">

            <ListBoxItem Content="Red" />

            <ListBoxItem Content="Green" Name="ListBoxItem2" />

            <ListBoxItem Content="Blue" Name="ListBoxItem1" />

            <ListBoxItem Content="Pink" />

            <ListBoxItem Content="Black" />

        </ListBox>

        <Button Content="Unselect All" Height="28" HorizontalAlignment="Right" Margin="0,135,320,0" Name="Button1" VerticalAlignment="Top" Width="75" />

        <Button Content="Select All" HorizontalAlignment="Left" Margin="10,135,0,148" Name="Button2" Width="75" />

    </Grid>

</Window>

Now double click on the select all and unselect all and add the following code.

Class MainWindow

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click

        ListBox1.SelectAll()

    End Sub

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

        ListBox1.UnselectAll()

    End Sub

End Class

Now run the application and click on the select all button to select all items from the ListBox.

l2.gif

Figure2.gif

Now click on the unselect all Button to unselect the items from the ListBox.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.