winform中的listbox实现拖拽功能

文章目录


前言

winform中的listBox实现拖拽!


一、实现

winform中的listbox实现拖拽只需要实现四个事件

1、准备两个listbox控件

其中listtarget,AllowDrop属性设置为True。

2、实现四个事件

2.1MouseDown

c 复制代码
//在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation.DragSize,则启动拖动动作。
private void ListSource_MouseDown(object sender, MouseEventArgs e)
{
    //ListBox中Item项的索引
    indexOfItemUnderMouseToDrag = listSource.IndexFromPoint(e.X, e.Y);
    //鼠标悬停在列表框的​​有效项​​上(即 indexOfItemUnderMouseToDrag不是 -1)
    if (indexOfItemUnderMouseToDrag != ListBox.NoMatches)
    {
        //记录鼠标按下位置,DragSize获取以鼠标按钮的按下点为中心的矩形的宽度和高度,在该矩形内不会开始拖动操作。
        Size dragSize =  SystemInformation.DragSize;
        //创建一个矩形区域(正方形)。以鼠标按下电为中心,以DragSize为高和宽的矩形。
        dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
    }
    else
    {
        //如果鼠标没有选中ListBox项,则置矩形区域为空
        dragBoxFromMouseDown = Rectangle.Empty;
    }
}

2.2MouseMove

c 复制代码
private void ListSource_MouseMove(object sender, MouseEventArgs e)
{
    /*
     * 鼠标按钮状态是用​​位标志(bit flags)​​存储的:

        每个按钮对应一个二进制位
        可以同时检测多个按钮的状态
        &(按位与)操作可以提取特定按钮的状态
     * */
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
        if (dragBoxFromMouseDown != Rectangle.Empty &&
            !dragBoxFromMouseDown.Contains(e.X,e.Y)
            )
        {
            //传递ListBox选中项并触发DoDragDrop事件(这里可以是ListDragSoure触发,也可以是ListDragTarget)
            //DoDragDrop 方法确定当前光标位置下的控件。然后它将检查该控件是否是有效的放置目标。
            DragDropEffects dropEffect = listSource.DoDragDrop(listSource.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link);
            if(dropEffect != DragDropEffects.None)
            {
                listSource.Items.RemoveAt(indexOfItemUnderMouseToDrag);
                if (indexOfItemUnderMouseToDrag > 0)
                    listSource.SelectedIndex = indexOfItemUnderMouseToDrag - 1;
                else if (listSource.Items.Count > 0)
                    listSource.SelectedIndex = 0;
            }

        }
    }
}

2.3DragDrop

c 复制代码
private void Listtarget_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(String)))
    {
        Object item = e.Data.GetData(typeof(String));
        if(e.Effect == DragDropEffects.Copy || 
            e.Effect == DragDropEffects.Move
            )
        {
            listtarget.Items.Add(item);
        }
    }
}

2.4DragEnter

c 复制代码
/*
当用户拖动数据进入目标控件(例如一个ListBox或其他控件)时,此方法会被调用。
通过设置e.Effect = DragDropEffects.Move;,它告诉系统当前控件接受拖放操作,并且操作的类型是"移动"。
这个设置会改变鼠标光标的显示(通常会显示一个移动图标),给用户视觉反馈,表明松开鼠标后数据将被移动到这里。
 */
private void Listtarget_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

2.5绑定事件

c 复制代码
private int indexOfItemUnderMouseToDrag;
private Rectangle dragBoxFromMouseDown;
public Form1()
{
    InitializeComponent();

    listSource.Items.AddRange(new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" });
    listSource.MouseDown += ListSource_MouseDown;
    listSource.MouseMove += ListSource_MouseMove;
    listtarget.DragDrop += Listtarget_DragDrop;
    listtarget.DragEnter += Listtarget_DragEnter;
}

效果图

注意:如果你的窗体内未使用Combox控件,则看这篇文章就行了。

如果使用到了Combox控件,建议看一下这篇文章"解决winform中的listbox实现拖拽时,遇到combox控件会闪烁的问题"。

相关推荐
ccut 第一混11 分钟前
c# winform 调用 海康威视工业相机(又全又细又简洁)
开发语言·c#·工业相机·海康威视
伽蓝_游戏5 小时前
UGUI源码剖析(3):布局的“原子”——RectTransform的核心数据模型与几何学
ui·unity·架构·c#·游戏引擎·游戏程序·几何学
王维志9 小时前
⏱ TimeSpan:C#时间间隔结构
前端·后端·c#·.net
chenglin01613 小时前
制造业ERP系统升级方案(C#到Java)
java·开发语言·c#
疯狂的Alex13 小时前
未来20年哪几种编程语言会保持优势?哪几种编程语言会得到更广泛的应用?
java·开发语言·c++·python·c#
mailtolaozhao18 小时前
C#入门--Hello world
开发语言·c#
王维志18 小时前
C# 中的 DateTime
开发语言·c#·.net
驾驭人生18 小时前
C#中统计某个字符出现次数的最简单方法
c#
yngsqq1 天前
cad c#二次开发 图层封装 获取当前层
java·数据库·c#