Click here to Skip to main content
15,943,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to add selected(more than one) items from one ListBox to another. I have done the following code but I'm not getting exactly what I want:
C#
protected void btn1_Click(object sender, EventArgs e)
{
    if (lst_employee.SelectedIndex > -1)
    {
        string value = lst_employee.SelectedItem.Value;
        string text = lst_employee.SelectedItem.Text;
        ListItem li = new ListItem();
        li.Text = text;
        li.Value = value;
        lstselected.Items.Add(li);
        lst_employee.Items.Remove(li);
    }
}
protected void btn2_Click(object sender, EventArgs e)
{
    if (lstselected.SelectedIndex > -1)
    {
        string value = lstselected.SelectedItem.Value;
        string text = lstselected.SelectedItem.Text;
        ListItem li1 = new ListItem();
        li1.Value = value;
        li1.Text = text;
        lstselected.Items.Remove(li1);
        lst_employee.Items.Add(li1);
    }
}
protected void btnall_Click(object sender, EventArgs e)
{
    int count = lst_employee.Items.Count;
    if (count != 0)
    {
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Text = lst_employee.Items[i].Text;
            item.Value = lst_employee.Items[i].Value;
            lstselected.Items.Add(item);
        }
    }
    lst_employee.Items.Clear();
}
protected void btnremove_Click(object sender, EventArgs e)
{
    int count = lstselected.Items.Count;
    if (count != 0)
    {
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Text = lstselected.Items[i].Text;
            item.Value = lstselected.Items[i].Value;
            lst_employee.Items.Add(item);
        }
    }
    lstselected.Items.Clear();
}

I want more than one selected items to be added and removed from one ListBox to another.

Hope you will help me.
Thanks in advance.
Posted
Updated 16-Aug-10 1:53am
v2
Comments
Samuel Cherinet 16-Aug-10 9:53am    
unless you are planning on binding this control to a datasource. I would just use ListView instead. you will get more flexibility and a typed item from it. something to think about!!
herry00742 17-Aug-10 5:15am    
i havent binded listboxes to databases but i manually added the items...in short i want all selected items to be added to other listbox using single button click.and those items must be removed from the first listbox in which they were originally...hope u ll get and reply me as early as possible...

Hard to know based on your post, which does not say what it is you want it to do extra, but I'd guess that you want to iterate over the selected items collection so that all the items you selected get moved.
 
Share this answer
 
You are using SelectedIndex property of your ListBox-es.
For single-selection ListBox this is fine but for multi-selection ListBox you have to use SelectedIndices and/or SelectedItems properties.
Check out the documentation of ListBox.SelectedIndex[^] and read the remarks section. There you will find useful information and links.

:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900