使用拖放重新排序winforms列表框?

Reorder a winforms listbox using drag and drop?(使用拖放重新排序winforms列表框?)

本文介绍了使用拖放重新排序winforms列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的过程吗?

Is this a simple process?

我只是为内部工具编写一个快速的 hacky 用户界面.

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

我不想花太多时间在上面.

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

推荐答案

这是一个快速而肮脏的应用程序.基本上我创建了一个带有按钮和列表框的表单.单击按钮时,ListBox 将填充接下来 20 天的日期(必须使用某些东西来进行测试).然后,它允许在 ListBox 中拖放以进行重新排序:

Here's a quick down and dirty app. Basically I created a Form with a button and a ListBox. On button click, the ListBox 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 ListBox 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列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用拖放重新排序winforms列表框?

基础教程推荐