首页 文章

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

提问于
浏览
8

我正在尝试将列表框1中的选定项目移动到列表框2,反之亦然 . 我有两个按钮, >><< . 当我在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);
}

7 回答

  • 10
    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);
        }
    }
    
  • 0

    你的代码工作正常 . 我测试了它 . 你的问题是“我试图将列表框1中的 selected 项目移到列表框2中 . ”

    我认为你的button2有problem.delete 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;

  • -2

    每个已删除的行都会有冲突,因此请使用以下代码:

    >>

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

    有关更多类型的答案,您可以使用此link

  • -2

    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

    我用这个代码解决了:

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

    获取使用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()
    }
    

相关问题