两个ListBox中的项互相移动及上下移动

好像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( " 请选择 ");
        });
    }
}
没注释,估计从Button名字应该能看出来。
url: http://greatverve.cnblogs.com/archive/2011/09/13/listbox-add-remove-up-down.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值