两个ListBox中的项互相移动及上下移动
本文参考:http://www.cnblogs.com/greatverve/archive/2012/03/27/listbox-add-remove-up-down.html
好像CodeProject里有功能非常强大的类似控件,这里没必要用自定义控件。
左右移动就是简单的选择项增加删除,上下移动使用了高级语法,值得一学。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
public static class ListBoxExtension
{
public static bool MoveSelectedItems(this WinForm.ListBox listBox, bool isUp, Action noSelectAction)
{
if (listBox.SelectedItems.Count > 0)
{
return listBox.MoveSelectedItems(isUp);
}
else
{
noSelectAction();
return false;
}
}
public static bool MoveSelectedItems(this WinForm.ListBox listBox, bool isUp)
{
bool result = true;
WinForm.ListBox.SelectedIndexCollection indices = listBox.SelectedIndices;
if (isUp)
{
if (listBox.SelectedItems.Count > 0 && indices[0] != 0)
{
foreach (int i in indices)
{
result &= MoveSelectedItem(listBox, i, true);
}
}
}
else
{
if (listBox.SelectedItems.Count > 0 && indices[indices.Count - 1] != listBox.Items.Count - 1)
{
for (int i = indices.Count - 1; i >= 0; i--)
{
result &= MoveSelectedItem(listBox, indices[i], false);
}
}
}
return result;
}
public static bool MoveSelectedItem(this WinForm.ListBox listBox, bool isUp, Action noSelectAction)
{
if (listBox.SelectedItems.Count > 0)
{
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
}
else
{
noSelectAction();
return false;
}
}
public static bool MoveSelectedItem(this WinForm.ListBox listBox, bool isUp)
{
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
}
private static bool MoveSelectedItem(this WinForm.ListBox listBox, int selectedIndex, bool isUp)
{
if (selectedIndex != (isUp ? 0 : listBox.Items.Count - 1))
{
object current = listBox.Items[selectedIndex];
int insertAt = selectedIndex + (isUp ? -1 : 1);
listBox.Items.RemoveAt(selectedIndex);
listBox.Items.Insert(insertAt, current);
listBox.SelectedIndex = insertAt;
return true;
}
return false;
}
}
{
public static bool MoveSelectedItems(this WinForm.ListBox listBox, bool isUp, Action noSelectAction)
{
if (listBox.SelectedItems.Count > 0)
{
return listBox.MoveSelectedItems(isUp);
}
else
{
noSelectAction();
return false;
}
}
public static bool MoveSelectedItems(this WinForm.ListBox listBox, bool isUp)
{
bool result = true;
WinForm.ListBox.SelectedIndexCollection indices = listBox.SelectedIndices;
if (isUp)
{
if (listBox.SelectedItems.Count > 0 && indices[0] != 0)
{
foreach (int i in indices)
{
result &= MoveSelectedItem(listBox, i, true);
}
}
}
else
{
if (listBox.SelectedItems.Count > 0 && indices[indices.Count - 1] != listBox.Items.Count - 1)
{
for (int i = indices.Count - 1; i >= 0; i--)
{
result &= MoveSelectedItem(listBox, indices[i], false);
}
}
}
return result;
}
public static bool MoveSelectedItem(this WinForm.ListBox listBox, bool isUp, Action noSelectAction)
{
if (listBox.SelectedItems.Count > 0)
{
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
}
else
{
noSelectAction();
return false;
}
}
public static bool MoveSelectedItem(this WinForm.ListBox listBox, bool isUp)
{
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
}
private static bool MoveSelectedItem(this WinForm.ListBox listBox, int selectedIndex, bool isUp)
{
if (selectedIndex != (isUp ? 0 : listBox.Items.Count - 1))
{
object current = listBox.Items[selectedIndex];
int insertAt = selectedIndex + (isUp ? -1 : 1);
listBox.Items.RemoveAt(selectedIndex);
listBox.Items.Insert(insertAt, current);
listBox.SelectedIndex = insertAt;
return true;
}
return false;
}
}
public partial class FrmReportSet : Form
{
public FrmReportSet()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
List<Object> listObj = new List<object>();
foreach (Object obj in lboxCanUse.SelectedItems)
{
lboxSelected.Items.Add(obj);
listObj.Add(obj);
}
foreach (Object obj in listObj)
{
lboxCanUse.Items.Remove(obj);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
List<Object> listObj = new List<object>();
foreach (Object obj in lboxSelected.SelectedItems)
{
lboxCanUse.Items.Add(obj);
listObj.Add(obj);
}
foreach (Object obj in listObj)
{
lboxSelected.Items.Remove(obj);
}
}
private void btnUp_Click(object sender, EventArgs e)
{
this.lboxSelected.MoveSelectedItems(true, () =>
{
MessageBox.Show("请选择");
});
}
private void btnDown_Click(object sender, EventArgs e)
{
this.lboxSelected.MoveSelectedItems(false, () =>
{
MessageBox.Show("请选择");
});
}
}
{
public FrmReportSet()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
List<Object> listObj = new List<object>();
foreach (Object obj in lboxCanUse.SelectedItems)
{
lboxSelected.Items.Add(obj);
listObj.Add(obj);
}
foreach (Object obj in listObj)
{
lboxCanUse.Items.Remove(obj);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
List<Object> listObj = new List<object>();
foreach (Object obj in lboxSelected.SelectedItems)
{
lboxCanUse.Items.Add(obj);
listObj.Add(obj);
}
foreach (Object obj in listObj)
{
lboxSelected.Items.Remove(obj);
}
}
private void btnUp_Click(object sender, EventArgs e)
{
this.lboxSelected.MoveSelectedItems(true, () =>
{
MessageBox.Show("请选择");
});
}
private void btnDown_Click(object sender, EventArgs e)
{
this.lboxSelected.MoveSelectedItems(false, () =>
{
MessageBox.Show("请选择");
});
}
}
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET