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

前端 未结 7 1556
闹比i
闹比i 2020-12-06 02:41

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 lis

相关标签:
7条回答
  • 2020-12-06 03:12

    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;

    0 讨论(0)
  • 2020-12-06 03:16

    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

    0 讨论(0)
  • 2020-12-06 03:20

    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(); 
    
    0 讨论(0)
  • 2020-12-06 03:21
    <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>
    
    0 讨论(0)
  • 2020-12-06 03:24

    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()
    }
    
    0 讨论(0)
  • 2020-12-06 03:27

    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]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题