首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两个ListBox中的项互相移动及上下移动

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

作者头像
跟着阿笨一起玩NET
发布2018-09-18 15:49:31
8810
发布2018-09-18 15:49:31
举报

好像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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-03-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档