User-ordered list
Hi,
I have a list that I want the user to order via the GUI. What I had in mind is a control that resembles a List box populated with items, so the the user can click and drag them into order. What control should I use for this? It is a common type of control but I can't seem to find a pre-defined control doing exactly this.
Thanks..
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Former memberPosted Jun 21, 2007, 6:33 AM EDT
I could be wrong and someone please correct me if I am, but I dont think it can be done without some serious coding.
Mike GoldRank #50 · 30.6K pointsPosted Jun 21, 2007, 12:14 PM EDT
Best,
-Mike
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DraggableListBox { public partial class DraggableListBoxComponent : ListBox { public DraggableListBoxComponent() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { // TODO: Add custom paint code here // Calling the base class OnPaint base.OnPaint(pe); } private string _dragData = ""; private int _selectedDragItemIndex = -1; private void DraggableListBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text, true)) { e.Effect = DragDropEffects.All; } } private void DraggableListBox_DragDrop(object sender, DragEventArgs e) { Point p = this.PointToClient(new Point(e.X, e.Y)); int index = this.IndexFromPoint(p); if (index >= 0) { Items.RemoveAt(_selectedDragItemIndex); Items.Insert(index, _dragData); } } private void DraggableListBoxComponent_SelectedIndexChanged(object sender, EventArgs e) { } private void DraggableListBoxComponent_MouseDown(object sender, MouseEventArgs e) { _selectedDragItemIndex = this.IndexFromPoint(e.X, e.Y); if (_selectedDragItemIndex >= 0) _dragData = (string)Items[_selectedDragItemIndex]; this.DoDragDrop(Items[this.SelectedIndex].ToString(), DragDropEffects.Move); } } }Ruan Rothmann11 pointsPosted Jun 24, 2007, 11:58 PM EDT