In this article I am going to show how we can transfer
data from one list to another list and how we can remove item from the list. In
this article I am using 2 List box and two button. One to Add Item from left
List to Right List and second is Remove button to remove item from right List
to left List.
I am using a class City to bind Left List Box.
Below is my Out put
Here I am Binding Left List Box with some cities name..

Image 1.
If you don't select any city name and click on Add then a Message box will appear..

Image 2.
Now Select an Item and click on Add...

Image 3.
Now If you want to remove an item from right list box and
you click on Remove without selecting any item...then

Image 4.
Now Select an item and click on remove ....

Image 5.
Below
is my XAML Code...
<Window x:Class="TransferDataFromOneList2AnotherList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Transfer Item From
One List 2 Another List" Height="310" Width="717" Background="LightBlue">
<Grid>
<ListBox Margin="91,32,0,40" Name="LeftListBox" HorizontalAlignment="Left" Width="168" Background="AntiqueWhite" />
<ListBox Margin="0,31,89,41" Name="RightListBox" HorizontalAlignment="Right" Width="176" Background="AntiqueWhite" />
<Button Name="AddButton" Height="27" Margin="292,97,294,0" VerticalAlignment="Top"
Click="AddButton_Click">Add >></Button>
<Button Name="RemoveButton" Margin="292,0,294,100"
Click="RemoveButton_Click" Height="28" VerticalAlignment="Bottom"><< Remove</Button>
</Grid>
</Window>
Below
is my City Class...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TransferDataFromOneList2AnotherList
{
public class City
{
public string
CityName
{
get;
set;
}
}
}
My
XAML.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TransferDataFromOneList2AnotherList
{
/// <summary>
/// Interaction logic for
Window1.xaml
/// </summary>
public partial class Window1 : Window
{
List<City>
CityList;
public Window1()
{
InitializeComponent();
BindLeftList();
}
int currentItemIndex;
string currentItemText;
private void
BindLeftList()
{
CityList = new List<City> {
new City{CityName="Noida"},
new City{CityName="Delhi"},
new City{CityName="Saharanpur"},
new City{CityName="Jaspur"},
new City{CityName="Moradabad"},
new City{CityName="Agra"},
new City{CityName="Bariely"},
new City{CityName="Kanpur"},
new City{CityName="Greater Noida"}
};
LeftListBox.ItemsSource = CityList.Select(x => x.CityName);
}
private void
AddButton_Click(object sender, RoutedEventArgs e)
{
if (LeftListBox.SelectedIndex > -1)
{
currentItemText = LeftListBox.SelectedValue.ToString();
currentItemIndex = LeftListBox.SelectedIndex;
RightListBox.Items.Add(currentItemText);
if (CityList != null)
{
CityList.RemoveAt(currentItemIndex);
LeftListBox.ItemsSource = null;
LeftListBox.ItemsSource = CityList.Select(x => x.CityName);
}
}
else
{
System.Windows.MessageBox.Show("Please Select An Item To Remove");
}
}
private void
RemoveButton_Click(object sender, RoutedEventArgs e)
{
if (RightListBox.SelectedIndex > -1)
{
currentItemText = RightListBox.SelectedValue.ToString();
currentItemIndex = RightListBox.SelectedIndex;
City city = new City();
city.CityName = currentItemText;
CityList.Add(city);
RightListBox.Items.RemoveAt(RightListBox.Items.IndexOf(RightListBox.SelectedItem));
LeftListBox.ItemsSource = null;
LeftListBox.ItemsSource = CityList.Select(x => x.CityName);
}
else
{
System.Windows.MessageBox.Show("Please Select An Item To Remove");
}
}
}
}