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控件会闪烁的问题"。

相关推荐
zdr尽职尽责10 小时前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
BruceNeter2 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
何以解忧唯有撸码3 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手4 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库4 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc4 天前
C# 发展历史
开发语言·c#
kyle~5 天前
C# --- 反射
开发语言·c#
暴走约伯6 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316346 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船7 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua