在C# Winform中将选定的项目从一个列表框移动到另一个列表框

13

我正在尝试将列表框1中的选定项目移动到列表框2中,反之亦然。我有两个按钮,>><<。当我在列表框1中选择项目,然后单击>>时,这些项目应该从列表框1移动到列表框2。

private void MoveListBoxItems(ListBox source, ListBox destination)
{
    ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
    foreach (var item in sourceItems)
    {
        destination.Items.Add(item);
    }
    while (source.SelectedItems.Count > 0)
    {
        source.Items.Remove(source.SelectedItems[0]);
    }
}

private void button2_Click_1(object sender, EventArgs e)
{
    MoveListBoxItems(listbox , lstActivity);
}

1
"button2_Click_1" 这是问题。 - Habib Zare
删除按钮2,然后创建新的其他按钮。 - Habib Zare
7个回答

16

你的代码运行良好,我已经测试过了。 你的问题是:“我尝试将列表框1中的选定的项目移动到列表框2中。”

我认为你的button2有问题。删除button2和下面的代码。

private void button2_Click_1(object sender, EventArgs e)
{
    MoveListBoxItems(listbox , lstActivity);
}

然后创建其他按钮并创建点击事件。

完整源代码:

private void MoveListBoxItems(ListBox source, ListBox destination)
{
    ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
    foreach (var item in sourceItems)
    {
        destination.Items.Add(item);
    }
    while (source.SelectedItems.Count > 0)
    {
        source.Items.Remove(source.SelectedItems[0]);
    }
}

private void first2second_Click(object sender, EventArgs e)
{
    MoveListBoxItems(FirstListbox, LastListbox);
}

private void second2first_Click(object sender, EventArgs e)
{
    MoveListBoxItems(LastListbox, FirstListbox);
}

这段代码可以正常运行。如果您想选择多个项目,请将属性 SelectionMode 更改为 MultiSimple。


出现错误:附加信息:在使用ItemsSource时,操作无效。请改用ItemsControl.ItemsSource访问和修改元素。 - NoWar
在需要按引用传递时,请使用ref。即:private void MoveListBoxItems(ref ListBox source, ref ListBox destination)。 - RDSAGAR

2
private void buttonMoveToListBox1_Click(object sender, EventArgs e)
{
    if(listBox1.SelectedIndex != -1)
    {
        listBox2.Items.Add(listBox1.SelectedValue);
        listBox1.Items.Remove(listBox1.SelectedValue);
    }
}

private void buttonMoveToListBox2_Click(object sender, EventArgs e)
{
    if(listBox2.SelectedIndex != -1)
    {
        listBox1.Items.Add(listBox2.SelectedValue);
        listBox2.Items.Remove(listBox2.SelectedValue);
    }
}

ListBox不包含Add的定义! - user1770370
listbox.Items.Add 这只是一个打字错误。 - nawfal
抛出异常!:(“值不能为空”在写有的行中:listbox.items.add(listbox.selectValue); - user1770370

0

对于每一行删除操作都可能会产生冲突,因此请使用以下代码:

>>

     for (int intCount = ListBox1.SelectedItems.Count - 1; intCount >= 0; intCount--) 
     {
        ListBox2.Items.Add(ListBox1.SelectedItems[intCount]);
        ListBox1.Items.Remove(ListBox1.SelectedItems[intCount]);
     } 

<<

     for (int intCount = ListBox2.SelectedItems.Count - 1; intCount >= 0; intCount--)
     {
        ListBox1.Items.Add(ListBox2.SelectedItems[intCount]);
        ListBox2.Items.Remove(ListBox2.SelectedItems[intCount]);
     }     

如果上面那个不起作用,那么请尝试这个:
while (ListBox1.SelectedItems.Count > 0) 
{ 
    ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);  
    ListBox1.SelectedItems[0].Remove(); 
}

如果需要更多类型的答案,您可以使用链接


@nawfal 实际上我更喜欢这个,因为它给了我一些理解(至少对我来说是这样),不管怎样,我提供的链接包含了你所解释的方式 :) - Mr_Green
在 ListBox.Item.Remove 中出现异常!!!错误信息为:“索引超出了数组界限。” - user1770370
@user1770370,请查看我的编辑后的答案。尝试理解它(我还没有检查它)。 - Mr_Green

0

我使用以下代码解决了这个问题:

private void MoveListBoxItems(ListBox poSource, ListBox poDestination)
{
    while (poSource.SelectedItems.Count > 0)
    {
        poDestination.Items.Add(poSource.SelectedItems[0]);
        poSource.Items.Remove(poSource.SelectedItems[0]);
    }
}

0

-2

使用类似于get-content或get-adcomputer等命令,首先将项目获取到列表框2中,然后使用两个列表框(如listbox2和3)创建“>>”和“<<”按钮。

$buttonMOVERight_Click={
    foreach ($Srv in $listbox2.selectedItems)
        {$selectedServers=$Srv}
    $listbox3.BeginUpdate()
    foreach($TSrv in $Srv)
        {$listbox3.Items.Add($TSrv);
        $listbox2.Items.Remove($TSrv);}                         
    $listbox3.EndUpdate()
}

$buttonMoveLeft_Click={
        #TODO: Place custom script here
        foreach ($Srvs in $listbox3.selectedItems)
        {$ServersinRt=$Srvs}
    $listbox2.BeginUpdate()
    foreach($rSRv in $Srvs)
        {$listbox2.Items.Add($rSRv);
        $listbox3.Items.Remove($Srvs);}
        $listbox2.EndUpdate()
}

1
此答案不适用于Winforms。 - Chris

-2
<script type="text/javascript">
        $(document).ready(function() {
            // > arrow
            $('#SingleRightMove').click(function() {                    
                    $('#fromBox option:selected').remove().appendTo('#toBox');  
                    //$("#tobox option").attr("selected", false);        
                    $('#toBox').find("option").attr("selected", false); 

            });
            // < arrow
            $('#SingleLeftMove').click(function() {
                 $('#toBox option:selected').remove().appendTo('#fromBox');
                 $("#fromBox option").attr("selected", false);
            });
            // >> arrow
            $('#AllRightMove').click(function() {            
                 $('#fromBox option').remove().appendTo('#toBox');
                 $("#toBox option").attr("selected", false);
            });
            // << arrow
            $('#AllLeftMove').click(function() {           
                 $('#toBox option').remove().appendTo('#fromBox');
                 $("#fromBox option").attr("selected", false);
            });




        });



    </script>

1
嗯...这绝对不是这里所问问题的答案。问题是关于C#/Winforms的。 - Andrew Barber

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接