Wednesday, July 26, 2006

Adding and Removing items from a ListBox

This is a small piece of code which can help startup programmers in ASP.Net

Paste this code in the code section.



void cmdAdd_Click(object sender, EventArgs e)
{
ListItem LI = ListBox1.SelectedItem;

if (LI != null)
{
ListBox2.Items.Add(LI);
ListBox2.SelectedIndex = -1;
ListBox1.Items.Remove(LI);
ListBox1.SelectedIndex = -1;
}
}

void cmdRemove_Click(object sender, EventArgs e)
{
ListItem LI = ListBox2.SelectedItem;

if (LI != null)
{
ListBox1.Items.Add(LI);
ListBox1.SelectedIndex = -1;
ListBox2.Items.Remove(LI);
ListBox2.SelectedIndex = -1;
}
}




This example requires two listboxes named ListBox1 and ListBox2. At least one of them having some items.

0 Comments:

Post a Comment

<< Home