发现很多网站上有这种效果,下面来实现一下吧!

如下图,按>>可以将左边的内容全部移动右边列表框,按>可以将选中的一个或者多个移动到右边的列表框。

实现过程:

两个列表框(ListBox)之间的内容相互移动,可以单个,也可以多个的实现

>>按钮的事件

>>按钮事件
 foreach(ListItem i in ListBox1.Items)
{
string a = i.Text;
string b = i.Value;
ListBox2.Items.Add(new ListItem(a, b));

}
ListBox1.Items.Clear();

 

>按钮的事件可以用for来做:

 1    for (int i = 0; i < ListBox1.Items.Count; i++)
2 {
3 if (ListBox1.Items[i].Selected)
4 {
5 string a = ListBox1.Items[i].Text;
6 string b = ListBox1.Items[i].Value;
7 ListBox2.Items.Add(new ListItem(a, b));
8 ListBox1.Items.RemoveAt(i);
9 i--;
10 }
11 }

其实:>按钮的实现用最好的方法是 while循环:

1    while (ListBox1.SelectedIndex > -1)
2 {
3 if (ListBox1.SelectedItem.Selected)
4 {
5 ListBox2.Items.Add(ListBox1.SelectedItem);
6 }
7 ListBox1.Items.Remove(ListBox1.SelectedItem);
8 }

以上是实现移动全部和移动选中单个或者多个的方法,下面的从右边向左边移动,是一样的。在此就不在阐述。





 

相关文章:

  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-01-14
  • 2022-01-01
  • 2021-11-04
  • 2021-12-06
  • 2021-06-17
猜你喜欢
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
  • 2021-07-13
相关资源
相似解决方案