Step 3 : The BlankPage.xaml file is as in the following code:
Code :
<Page
x:Class="Application8.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Application8"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".133*"/>
<ColumnDefinition Width=".333*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=".033*"/>
<RowDefinition Height=".080*"/>
<RowDefinition Height=".333*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Dynamic Updation" FontSize="30" HorizontalAlignment="Right"></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Text=" in List Box" FontSize="30" HorizontalAlignment="Left"></TextBlock>
<TextBox x:Name="txt1" Background="Yellow" Grid.Column="0" Grid.Row="1" Height="50" Width="300"
HorizontalAlignment="Center" VerticalAlignment="Top">
</TextBox>
<Button Content="ADD" Background="Red" Grid.Column="1" Grid.Row="1" Height="50" Width="200"
Margin="0,0,0,0" VerticalAlignment="Top" Click="Button_Click_1">
</Button>
<Button Content="DELETE" Background="Green" Grid.Column="1" Grid.Row="1" Height="50" Width="200"
Margin="0,50,0,0" VerticalAlignment="Top" Click="Button_Click_2">
</Button>
<ListBox x:Name="listb1" Grid.Column="0" Grid.Row="2" Height="300" Width="300"
Background="LightCyan" VerticalAlignment="Top" HorizontalAlignment="Center" FontWeight="Bold" Foreground="Black" FontSize="25">
</ListBox>
</Grid>
</Page>
Step 4 : The BlankPage.xaml.cs file is as in the following code:
Code :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Application8
{
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
listb1.Items.Add(txt1.Text);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
listb1.Items.RemoveAt
(listb1.Items.IndexOf(listb1.SelectedItem));
}
}
}
Step 5 : After running this code the output looks like this:
![img3.gif]()
To add data to the list box enter the value into the text box and click on the add button.
![img4.gif]()
As simply you can add many items as you want.
![img5.gif]()
In the reverse process delete the items from list box, select the item and click on the delete button.
![img6.gif]()
You will find that the selected item has been removed from the list.
![img7.gif]()