Move selected items from one listbox to another in C# winform(在 C# winform 中将所选项目从一个列表框移动到另一个列表框)
问题描述
我正在尝试将列表框 1 中的选定项目移动到列表框 2,反之亦然.我有两个按钮,>>
和 <<
.当我选择 listbox1 中的项目然后单击 >>
时,项目应该从 listbox1 移动到 listbox2.
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 中的 选定 项移动到列表框 2."
your code works fine. i tested it. your question is "I try to move selected item in list box1 to list box2."
我认为你的 button2 有问题.删除 button2 和下面的代码
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.
完整来源:
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;
this code is work. if you want select more than one item change property SelectionMode = MultiSimple;
这篇关于在 C# winform 中将所选项目从一个列表框移动到另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# winform 中将所选项目从一个列表框移动到另一个列表框


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01