C#Winform 实现ListBox中项的拖拽

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/pp_fzp/article/details/53319533
 private void lstRead_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.lstRead.SelectedItem == null)
            {
                return;
            }
            //开始拖放操作,DragDropEffects为枚举类型。
            //DragDropEffects.Move 为将源数据移动到目标数据
            this.lstRead.DoDragDrop(this.lstRead.SelectedItem, DragDropEffects.Move);
        }

        private void lstRead_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void lstRead_DragDrop(object sender, DragEventArgs e)
        {
            Point point = lstRead.PointToClient(new Point(e.X, e.Y));
            int index = this.lstRead.IndexFromPoint(point);
            if (index < 0)
            {
                index = this.lstRead.Items.Count - 1;
            }
            //获取拖放的数据内容
            object data = e.Data.GetData(typeof(string));
            //删除原数据
            this.lstRead.Items.Remove(data);
            //插入目标数据
            this.lstRead.Items.Insert(index, data);
        }

展开阅读全文

没有更多推荐了,返回首页