两个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;
复制代码
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 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("请选择");
        });
    }
}
复制代码
posted @ 2013-02-06 11:06  跟着阿笨一起玩.NET  阅读(772)  评论(0编辑  收藏  举报