Move selected items from one listbox to another in

2019-01-14 19:00发布

问题:

I'm trying to move selected items in list box1 to list box2, and vice versa. I have two buttons, >> and <<. When I select items in listbox1 and then click on >> the items should move from listbox1 to 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);
}

回答1:

your code works fine. i tested it. your question is "I try to move selected item in list box1 to list box2."

i think your button2 has problem.delete button2 and the code below

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;



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


回答3:

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



回答4:

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(); currentItemIndex = LeftListBox.SelectedIndex;

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


回答5:

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


回答6:

<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>


回答7:

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..

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