简体   繁体   English

在 C# winform 中将所选项目从一个列表框移动到另一个列表框

[英]Move selected items from one listbox to another in C# winform

I'm trying to move selected items in list box1 to list box2, and vice versa.我正在尝试将列表框 1 中的选定项目移动到列表框 2,反之亦然。 I have two buttons, >> and << .我有两个按钮, >><< When I select items in listbox1 and then click on >> the items should move from listbox1 to listbox2.当我在 listbox1 中选择项目然后单击>>项目应该从 listbox1 移动到 listbox2。

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);
}

your code works fine.你的代码工作正常。 i tested it.我测试了它。 your question is "I try to move selected item in list box1 to list box2."您的问题是“我尝试将列表框 1 中的选定项目移动到列表框 2。”

i think your button2 has problem.delete button2 and the code below我认为你的 button2 有问题。删除 button2 和下面的代码

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

then create other button and create click event.然后创建其他按钮并创建点击事件。

full source:完整来源:

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);
}

this code is work.这段代码是工作。 if you want select more than one item change property SelectionMode = MultiSimple;如果要选择多个项目,请更改属性 SelectionMode = MultiSimple;

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);
    }
}

There will be conflicts for every deleted row, so go with the below code:每个删除的行都会有冲突,所以请使用以下代码:

>> >>

     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]);
     }     

If the above one doesn't work then try this:如果上述方法不起作用,请尝试以下操作:

while (ListBox1.SelectedItems.Count > 0) 
{ 
    ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);  
    ListBox1.SelectedItems[0].Remove(); 
}

For more type of answers you can go with this link有关更多类型的答案,您可以使用此链接

Here Two ListBoxes.这里有两个列表框。 When i select the name from Left listbox and click ">>" Button the data move to Right ListBox当我从左列表框中选择名称并单击“>>”按钮时,数据将移动到右列表框

Code.. currentItemText = LeftListBox.SelectedValue.ToString();代码.. currentItemText = LeftListBox.SelectedValue.ToString(); currentItemIndex = LeftListBox.SelectedIndex; currentItemIndex = LeftListBox.SelectedIndex;

        RightListBox.Items.Add(currentItemText);
        if (myDataList != null)
        {
            myDataList.RemoveAt(currentItemIndex);
        }
        ApplyDataBinding(); 

I solved using this code:我使用此代码解决了:

private void MoveListBoxItems(ListBox poSource, ListBox poDestination)
{
    while (poSource.SelectedItems.Count > 0)
    {
        poDestination.Items.Add(poSource.SelectedItems[0]);
        poSource.Items.Remove(poSource.SelectedItems[0]);
    }
}
<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>

Get the >> and << buttons created with 2 list boxes like listbox2 and 3 below...get ur items in list box 2 first by something like get-content or get-adcomputer etc..获取使用 2 个列表框创建的 >> 和 << 按钮,例如下面的 listbox2 和 3...首先通过 get-content 或 get-adcomputer 等获取列表框 2 中的项目。

$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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM