【问题标题】:Move selected items from one listbox to another in C# winform在 C# winform 中将所选项目从一个列表框移动到另一个列表框
【发布时间】:2012-10-31 21:59:42
【问题描述】:

我正在尝试将列表框 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);
}

【问题讨论】:

  • "button2_Click_1" 这就是问题所在。
  • 删除按钮2,然后创建新的其他按钮。

标签: c# winforms listbox


【解决方案1】:
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 的定义!
  • listbox.Items.Add 这只是一个错字
  • Give Exception!!!:( "Value can not be null" in Line that writes : listbox.items.add(listbox.selectValue);
【解决方案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

【讨论】:

  • @nawfal 实际上我更喜欢这个,因为它提供了一些理解(至少对我而言),无论如何我提供的内容包含你解释的方式:)
  • 在 ListBox.Item.Remove 中给出异常!!!它是:“索引超出了数组的范围。
  • @user1770370 检查我编辑的答案。试着理解一下(我没查过)
【解决方案3】:

您的代码运行良好。我测试了它。 您的问题是“我尝试将列表框 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 访问和修改元素。
  • 使用 ref 以防您需要通过引用传递。即 private void MoveListBoxItems(ref ListBox source, ref ListBox destination)
【解决方案4】:
<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>

【讨论】:

  • 嗯...这绝对不是对这里提出的问题的回答。问题是关于 C#/Winforms。
【解决方案5】:

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

【讨论】:

  • 此答案不适用于 Winforms。
【解决方案6】:

Here Two ListBoxes. When i select the name from Left listbox and click ">>" Button the data move to Right ListBox

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

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

【讨论】:

    【解决方案7】:

    我用这段代码解决了:

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

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多