C# ListBox 左移、右移、上移、下移

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:03   1117   0

C# ListBox 左移、右移、上移、下移

2012-11-17 22:53:45| 分类: 技术研讨 | 标签:listbox |字号 订阅

/// <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涉及其事件时,就要加条件!

转载于:https://www.cnblogs.com/meimao5211/p/3346318.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP