Ottobre 5th, 2007
->
Description: One function for adding items, and one for removing items. Just add the functions in your code and call them with the name of the listbox as parameter.
Link: http://www.codekeep.net/snippets/e870819e-1ce0-47e6-818f-8feebf3f570b.aspx
/// <summary>
/// Adding selected item from one listbox to another.
/// If listBoxTarget already has the value selected in listBoxSource, it will not be added.
///
/// Example how to call:
/// AddSelectedItem(listBox1, listBox2);
/// </summary>
/// <param name="listBoxSource"></param>
/// <param name="listBoxTarget"></param>
private static void AddSelectedItem(ListBox listBoxSource, ListBox listBoxTarget)
{
if (listBoxSource.SelectedIndex != -1 && !listBoxTarget.Items.Contains(listBoxSource.SelectedItem))
{
listBoxTarget.Items.Add(listBoxSource.SelectedItem);
}
}
/// <summary>
/// Removes the selected item from the given listbox.
///
/// Example how to call:
/// RemoveSelectedItem(listBox2);
/// </summary>
/// <param name="ListBoxName">Name of the listbox.</param>
private static void RemoveSelectedItem(ListBox ListBoxName)
{
//Is there a selected item in the listbox?
if (ListBoxName.SelectedIndex != -1)
{
ListBoxName.Items.Remove(ListBoxName.SelectedItem);
}
}