C# 将项目从一个列表框转移到另一个列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19576802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:20:17  来源:igfitidea点击:

transfer items from one listbox to another

c#winformslistbox

提问by Haider Khattak

so far i have accomplished transfer a single select item from one lb1 to lb2 and vice versa. Now the problem is transfering the whole list of data from lb1 to lb2 and vice versa. if anyone could help please. using for loop will be much better.

到目前为止,我已经完成了从一个 lb1 到 lb2 的单个选择项目的转移,反之亦然。现在的问题是将整个数据列表从 lb1 传输到 lb2,反之亦然。如果有人可以帮忙,请。使用 for 循环会好得多。

i am using the following code:

我正在使用以下代码:

    private void add_Click(object sender, EventArgs e)
    {
        if (lb1.SelectedItem != null)
        {
            lb2.Items.Add(lb1.SelectedItem);
            lb1.Items.Remove(lb1.SelectedItem);
        }
        else
        {
            MessageBox.Show("No item selected");
        }
    }

    private void remove_Click(object sender, EventArgs e)
    {
        if (lb2.SelectedItem != null)
        {
            lb1.Items.Add(lb2.SelectedItem);
            lb2.Items.Remove(lb2.SelectedItem);
        }
        else
        {
            MessageBox.Show("No item selected");
        }
    }

    private void addall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb1 to lb2
    {
        for (int i = 0; i < lb1.SelectedItems.Count; i++)
        {
            lB2.Items.Add(lb1.SelectedItems[i].ToString());
        }
    }

    private void removeall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb2 to lb1
    {

    }

采纳答案by User 12345678

Because you want to transfer allof the items from one list box to another not only the selected items you must loop for every item in the list box and not only the selected items.

因为您想将所有项目从一个列表框中传输到另一个列表框中,而不仅仅是所选项目,您必须为列表框中的每个项目循环,而不仅仅是所选项目。

So change your usage of ListBox.SelectedItemsto ListBox.Itemsand your code should work as expected assuming you remember to remove the items as well.

因此,如果您还记得删除这些项目,那么更改ListBox.SelectedItemsto的用法ListBox.Items并且您的代码应该可以按预期工作。

for (int i = 0; i < lb1.Items.Count; i++)
{
    lB2.Items.Add(lb1.Items[i].ToString());
}
lb1.Items.Clear();

回答by PiousVenom

You could do something like this:

你可以这样做:

private void addall_Click(object sender, EventArgs e)
{
    foreach (var item in lb1.Items)
    {
        lB2.Items.Add(item);
    }
}

This would loop through your list and add them all. You would do the reverse for lb2->lb1.

这将遍历您的列表并将它们全部添加。您将对lb2->lb1.

回答by Philip W

Simply iterate over all Items of the list and add each element to the next list. At the end simply remove all Items.

只需遍历列表的所有项目并将每个元素添加到下一个列表。最后只需删除所有项目。

foreach (var item in listBox1.Items)
{
    listBox2.Items.Add(item);
}
listBox1.Items.Clear();