开发者

Cloning items in a listbox c#

I have 2 list boxes and want to be able to copy selected items from one to the other how ever many开发者_如何学Go 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)


It seems like you're travelling around the world to do something simple. I would approach this using List and databinding the list.

// Add code
        DataRowView selected = listBox1.SelectedItem as DataRowView;
        if (selected != null)
        {
            _myList.Add(selected); // Adds at end
            BindList2();
        }

// Move up code
    int selectedIndex = listBox2.SelectedIndex;
    if(selectedIndex > 0)
    {
        var temp = _myList[selectedIndex];
        _myList.Remove(temp);
        _myList.InsertAt(selectedIndex - 1, temp);
        BindList2();
    }

// BindList2
public void BindList2()
{
    listBox2.DataSource = _myList;
    listBox2.DataBind();
}


You can use SelectedIndex instead of SelectedItem when you have multiple items that are all equal. I also recommend checking that it's not -1.


The problem for the up case is the following set of code.

object selected = listBox2.SelectedItem;
int index = list2.Items.IndexOf(selected);

This code will only function correctly if you have unique items in the list. Once you have duplicate items the value index will be the index of the first instance of say gills in the list and not necessarily the index of the selected value.

It seems like you mirror the items in listBox2 and list2. If that is the case then you can just use the SelectedIndex property directly on listBox2 since the index will be equal in both liss.

int index = listBox2.SelectedIndex;


If you are trying to use an list of objects, try implementing the Iclonnable. This will make copies of the same item over & over. Also note to move an item to the top or bottom you don't have to remove the item in the list & reinsert them back. But you can change the index of the item. Hope this helps.


Just the code, because the rest of the answers cover it anyways:

 private void buttonAdd_Click(object sender, EventArgs e)
    {

        DataRowView selected = listBox1.SelectedItem as DataRowView;
        if (selected != null)
        {
            string item = selected["title"].ToString();
            listBox2.Items.Add(item);
        }
    }

    private void buttonUp_Click(object sender, EventArgs e)
    {
        string selected = listBox2.SelectedItem as string;
        int oldIndex = listBox2.SelectedIndex;
        int newIndex = oldIndex;

        if (!string.IsNullOrEmpty(selected) && listBox2.Items.Count > 1 && oldIndex > 0)
        {
            listBox2.SuspendLayout(); 

            listBox2.Items.RemoveAt(oldIndex);
            newIndex = oldIndex - 1;
            listBox2.Items.Insert(newIndex, selected);
            listBox2.SelectedIndex = newIndex;

            listBox2.ResumeLayout();

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜