Reorder a winforms listbox using drag and drop?

前端 未结 5 1939
不思量自难忘°
不思量自难忘° 2020-12-13 06:01

Is this a simple process?

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

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

5条回答
  •  离开以前
    2020-12-13 06:38

    7 Years Late. But for anybody new, here is the code.

    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 = listBox1.SelectedItem;
            this.listBox1.Items.Remove(data);
            this.listBox1.Items.Insert(index, data);
        }
    
        private void itemcreator_Load(object sender, EventArgs e)
        {
            this.listBox1.AllowDrop = true;
        }
    

提交回复
热议问题