如图从左边选择可以多选,然后点击下按钮添加到右边,然后左边的消失。右边同理。一个按钮把左边的全部添加到右边,右边同理。还有个按钮随机排列右边的内容。这样怎么做到?能给个例子吗?
解决方案
100
public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.SelectionMode = SelectionMode.MultiSimple; listBox2.SelectionMode = SelectionMode.MultiSimple; listBox1.Items.Add(1); ; listBox1.Items.Add(2); listBox1.Items.Add(3); listBox1.Items.Add(4); listBox1.Items.Add(5); button1.Click += new EventHandler(button_Click); button2.Click += new EventHandler(button_Click); button3.Click += new EventHandler(button_Click); button4.Click += new EventHandler(button_Click); } void button_Click(object sender, EventArgs e) { switch ((sender as Button).Text) { case ">": ItemMove(listBox1, listBox2); break; case "<": ItemMove(listBox2, listBox1); break; case ">>": ItemMove(listBox1, listBox2, true); break; case "<<": ItemMove(listBox2, listBox1, true); break; } } void ItemMove(ListBox a, ListBox b, bool all=false) { var st = new ArrayList(); if(all) foreach (var item in a.Items) st.Add(item); else foreach (var item in a.SelectedItems) st.Add(item); foreach(var item in st) { b.Items.Add(item); a.Items.Remove(item); } } }