通过拖放重新排列一个WinForms ListBox和下降? [英] Reorder a winforms listbox using drag and drop?

查看:125
本文介绍了通过拖放重新排列一个WinForms ListBox和下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的过程?

我只写了一个内部​​工具的快速哈克UI。

I'm only writing a quick hacky UI for an internal tool.

我不想花一个年龄就可以了。

I don't want to spend an age on it.

推荐答案

下面是一个快速下降和肮脏的应用程序。基本上,我创建了一个表格有一个按钮和一个ListView。在按一下按钮,列表视图被填充的未来20天日期(不得不用的东西只是用于测试)。然后,它允许阻力和列表视图重新排序中下降:

Here's a quick down and dirty app. Basically I created a Form with a button and a ListView. On button click, the listview gets populated with the date of the next 20 days (had to use something just for testing). Then, it allows drag and drop within the listview for reordering:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.listBox1.AllowDrop = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= 20; i++)
            {
                this.listBox1.Items.Add(DateTime.Now.AddDays(i));
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
        }

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

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            Point point = listBox1.PointToClient(new Point(e.X, e.Y));
            int index = this.listBox1.IndexFromPoint(point);
            if (index < 0) index = this.listBox1.Items.Count-1;
            object data = e.Data.GetData(typeof(DateTime));
            this.listBox1.Items.Remove(data);
            this.listBox1.Items.Insert(index, data);
        }

这篇关于通过拖放重新排列一个WinForms ListBox和下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆