/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxOperate_OnCommand(object sender, EventArgs e)
{
Button btn = sender as Button;
List<string> list = new List<string>();
switch (btn.Tag.ToString())
{
case "ToRight":
if (listBox1.SelectedItems != null)
{
foreach (var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item.ToString());
}
this.listBox2.SelectedIndex = this.listBox2.Items.Count - 1;
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
i--;
}
}
break;
case "AllToRight":
if (this.listBox1.Items.Count > 0)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
list.Add(listBox1.Items[i].ToString());
}
for (int j = 0; j < list.Count; j++)
{
this.listBox2.Items.Add(list[j]);
}
this.listBox1.Items.Clear();
break;
}
else
{
break;
}
case "ToLeft":
if (listBox2.SelectedItems != null)
{
foreach (var item in listBox2.SelectedItems)
{
listBox1.Items.Add(item.ToString());
}
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
for (int i = 0; i < listBox2.SelectedIndices.Count; i++)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
i--;
}
}
break;
case "AllToLeft":
if (this.listBox2.Items.Count > 0)
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
list.Add(listBox2.Items[i].ToString());
}
for (int j = 0; j < list.Count; j++)
{
this.listBox1.Items.Add(list[j]);
}
this.listBox2.Items.Clear();
break;
}
else
{
break;
}
case "ToUp":
// 上移
if (this.listBox2.SelectedIndices.Count > 0 &&
this.listBox2.SelectedIndices[0] > 0)
{
int[] newIndices =
this.listBox2.SelectedIndices.Cast<int>()
.Select(index => index - 1).ToArray();
this.listBox2.SelectedItems.Clear();
for (int i = 0; i < newIndices.Length; i++)
{
object obj = this.listBox2.Items[newIndices[i]];
this.listBox2.Items[newIndices[i]] = this.listBox2.Items[newIndices[i] + 1];
this.listBox2.Items[newIndices[i] + 1] = obj;
this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i]]);
}
}
break;
case "ToDown":
// 下移
if (this.listBox2.SelectedIndices.Count > 0 &&
this.listBox2.SelectedIndices[this.listBox2.SelectedIndices.Count - 1] <
this.listBox2.Items.Count - 1)
{
int[] newIndices =
this.listBox2.SelectedIndices.Cast<int>()
.Select(index => index + 1).ToArray();
this.listBox2.SelectedItems.Clear();
for (int i = newIndices.Length; i > 0; i--)
{
object obj = this.listBox2.Items[newIndices[i - 1]];
this.listBox2.Items[newIndices[i - 1]] = this.listBox2.Items[newIndices[i-1]-1];
this.listBox2.Items[newIndices[i-1]-1] = obj;
this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i-1]]);
}
}
break;
}
}
注意:此方法只是简单的顺序移动,如果某一listbox涉及其事件时,就要加条件!