Link to home
Start Free TrialLog in
Avatar of GPSAddict
GPSAddict

asked on

Listbox selections

Hi, I have this listbox1 populated by a database and next to it another empty one (listbox2). Between the two, 2 buttons "-->" & "<--". You guessed it I need those buttons to add and remove items from listbox1 to listbox2. One they have been removed from listbox1, I do not want those values to still be in it, it's really to move items between the two listboxes. What would be code to achieve this ?
Thanks
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Try this code.

Hope it's clear enough for u.
protected void btnAdd_Click(object sender, EventArgs e)
    {
        for (int i = lstFrom.Items.Count - 1; i >= 0; i--)
        {
            if (lstFrom.Items[i].Selected)
            {
                if (lstTo.Items.FindByValue(lstFrom.Items[i].Value) != null)
                {
                    continue;
                }
                else
                {
                    lstTo.Items.Add(lstFrom.Items[i]);
                    lstFrom.Items.Remove(lstFrom.Items[i]);
                }
            }
        }
 
    }
        
 
   
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = lstTo.Items.Count - 1; i >= 0; i--)
            {
                if (lstTo.Items[i].Selected)
                {
                    lstFrom.Items.Add(lstTo.Items[i]);
                    lstTo.Items.Remove(lstTo.Items[i]);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    } 

Open in new window

Avatar of GPSAddict
GPSAddict

ASKER

I'll try, the only problem is I don't know C, I use VB. Thanks
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alright, let me try this at home and I'll get back to you asap. Thanks !
2 things if I may, if you do a batch add, it adds the items but in reverse order ie. I select 1,2,3,4 but it adds in this order; 4,3,2,1. Also, when I remove an item from the 2nd listbox, how can I make it so that re-appears where it was before ? Thanks
Hello ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial