My solution was use two ListBoxes (one for the asigned items and one for the deallocated items) and put some buttons between them, you know the classic asign all, asign selection, deallocate all and finally deallocate all. To avoid writing multiple times the same move item code I created two gold rules to operate my list boxes:
1. You have to provide the items to the ListBoxes binding a generic List.
2. The items should be objects with a correct overriden Equals method.
The mandatory screenshot:
The events of all buttons:
private void asignAllButton_Click(object sender, EventArgs e)
{
moveAllItems(notAsignedListBox,asignedListBox);
}
private void asignSelectionButton_Click(object sender, EventArgs e)
{
moveSelectedItems<MyObject>(notAsignedListBox, asignedListBox);
}
private void deallocateSelectionButton_Click(object sender, EventArgs e)
{
moveSelectedItems<MyObject>(asignedListBox, notAsignedListBox);
}
private void deallocateAllButton_Click(object sender, EventArgs e)
{
moveAllItems<MyObject>(asignedListBox, notAsignedListBox);
}
The utility methods:
public static void moveSelectedItems<T>(ListBox source, ListBox destiny)
{
var selectedList = source.SelectedItems;
if (selectedList == null || selectedList.Count == 0)
{
return;
}
var destinyList = (List<T>)destiny.DataSource;
var sourceList = (List<T>)source.DataSource;
if (destinyList == null)
{
destinyList = new List<T>();
}
foreach (T generic in selectedList)
{
destinyList.Add(generic);
sourceList.Remove(generic);
}
destiny.DataSource = null;
source.DataSource = null;
destiny.DataSource = destinyList;
source.DataSource = sourceList;
}
public static void moveAllItems<T>(ListBox source, ListBox destiny)
{
if (source.DataSource == null)
{
return;
}
var destinyList = (List<T>)destiny.DataSource;
if (destinyList == null)
{
destinyList = new List<T>();
}
destinyList.AddRange((List<T>)source.DataSource);
destiny.DataSource = null;
destiny.DataSource = destinyList;
source.DataSource = null;
}
Now you have a cross object asignation component. God! My favorite programing language is Java but my blog is being invaded by C# posts ! :S
No hay comentarios:
Publicar un comentario