Cloning items in a listbox c#

Posted by Jenny on Stack Overflow See other posts from Stack Overflow or by Jenny
Published on 2010-03-13T17:15:47Z Indexed on 2010/03/13 17:55 UTC
Read the original article Hit count: 444

Filed under:
|
|

I have 2 list boxes and want to be able to copy selected items from one to the other how ever many times I want. Ive managed to do this but I have buttons on the 2nd list box that allow me to go up and down..Now when theres to items in the second list box that are the same (e.g "gills" and "gills") it doesnt behave normally and crashes.

Is there a way in which I can get them to act as seperate items in the 2nd listbox?

code

 private void buttonUp_Click(object sender, EventArgs e)
    {
        object selected = listBox2.SelectedItem;
        int index = list2.Items.IndexOf(selected);

        listBox2.Items.Remove(selected);
        listBox2.Items.Insert(index - 1, selected);
        listBox2.SetSelected(index - 1, true);
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {          
        DataRowView selected = (DataRowView)listBox1.SelectedItem;            
         string item  = selected["title"].ToString();
         listBox2.Items.Add(item);

    }

It works fine when i havnt got duplicates but when i do they just jump around randomly when i press up/down.

(ive not included down as its pretty much the same as up)

© Stack Overflow or respective owner

Related posts about c#

Related posts about listbox