winform的listbox拖动(拖拽)排序实例,源码在下方免积分下载

工具VS2013,框架.NET 2.0

下载地址:
http://download.csdn.net/detail/donggege214/9092703

简单介绍一下思路:
1、当鼠标点击时,记下源位置(indexofsource)
2、当拖动时,记下鼠标所指的目标位置(indexoftarget)
3、将源位置的项,插入到目标位置
4、删除源位置项

接下来简单介绍下步骤:
1、在窗口中添加一个listbox这里写图片描述
2、添加listbox的事件(在空白处双击就会自动生成事件)这里写图片描述
3、代码实现上述思路(贴的后台全部代码)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace List拖拽排序
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.AllowDrop = true;//允许ListBox拖动
            IniLB();//初始化ListBox
        }

        /// <summary>
        /// 在listbox中添加10个项
        /// </summary>
        public void IniLB()
        {
            for (int i = 0; i < 10; i++)
            {
                listBox1.Items.Add(i);
            }
        }

        /// <summary>
        /// 目标位置
        /// </summary>
        public int indexoftarget = -1;
        /// <summary>
        /// 源位置
        /// </summary>
        public int indexofsource = -1;




        /// <summary>
        /// 鼠标点击listbox项时,记下源位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            indexofsource = ((ListBox)sender).IndexFromPoint(e.X, e.Y);

            if (indexofsource != ListBox.NoMatches)
            {
                ((ListBox)sender).DoDragDrop(((ListBox)sender).Items[indexofsource].ToString(), DragDropEffects.Move);
            }
        }


        /// <summary>
        /// 拖动时,记录目标位置,进行交换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            ListBox listbox = (ListBox)sender;
            indexoftarget = listbox.IndexFromPoint(listbox.PointToClient(new Point(e.X, e.Y)));
            if (indexoftarget != ListBox.NoMatches)
            {
                //如果目标位置在源位置下方
                if (indexoftarget > indexofsource)
                {
                    listbox.Items.Insert(indexoftarget + 1, listbox.Items[indexofsource]);
                    listbox.Items.RemoveAt(indexofsource);
                    listbox.SelectedIndex = indexoftarget;
                }
                //如果目标位置在源位置上方
                else if (indexoftarget < indexofsource)
                {
                    listbox.Items.Insert(indexoftarget, listbox.Items[indexofsource]);
                    listbox.Items.RemoveAt(indexofsource + 1);
                    listbox.SelectedIndex = indexoftarget;
                }
                else
                { }


            }
        }


        /// <summary>
        /// 超出边界,禁止拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DragOver(object sender, DragEventArgs e)
        {
            //拖动源和放置的目的地一定是一个ListBox
            if (e.Data.GetDataPresent(typeof(System.String)) && ((ListBox)sender).Equals(listBox1))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
                e.Effect = DragDropEffects.None;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/donggege214/article/details/48318617
今日推荐