Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder a winforms listbox using drag and drop?

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.

like image 684
Gareth Simpson Avatar asked Apr 30 '09 02:04

Gareth Simpson


People also ask

How do you do drag and drop controls in C#?

Drag and Drop in C# Microsoft has added a series of properties and events to help you use drag and drop with your controls. You must set the AllowDrop property to allow for dragging and dropping within the tree view. Also, there are about 5 major events available for trapping drag-drop operations.

Can listbox be edited?

To edit an item in a listbox, all you need is a simple editbox that overlays on a listbox item. The idea is to create a TextBox control and keep it hidden and show it whenever required. Two events are added to the EditBox. KeyPress event to check if the enter key is pressed when the user has finished editing the item.


2 Answers

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);         } 
like image 170
BFree Avatar answered Sep 30 '22 08:09

BFree


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;     } 
like image 27
Wally Modz Avatar answered Sep 30 '22 09:09

Wally Modz