How to fill selected items from one list box to another on a click button action?

advertisements

I have two list boxes L1 and L2. Now on a button click method i have to move the selected items in L1 to L2 and Items in L1 should get Deleted.

  protected void Btn2_Click(object sender, EventArgs e)
    {
        string sel = LB1.SelectedValue;

        List<string> ab = new List<string>();

        ab.Add(sel);

        L2.Text = Convert.ToString(ab.Count);

        for(int i =0; i < ab.Count ; i++)
        {
            string c = ab[i];
            LB2.Items.Add(c);

        }


if you want to remove the SelectedItem from the ListBox1

add the following statement at the end of your function:

LB1.Items.Remove(LB1.SelectedValue);

Complete Code:

protected void Btn2_Click(object sender, EventArgs e)
{
    string sel = LB1.SelectedValue;

    List<string> ab = new List<string>();

    ab.Add(sel);

    L2.Text = Convert.ToString(ab.Count);

    for(int i =0; i < ab.Count ; i++)
    {
        string c = ab[i];
        LB2.Items.Add(c);
    }

   LB1.Items.Remove(LB1.SelectedValue);//Add This to remove selected item from ListBox1
}