Skip to content

User-ordered list

Ruan Rothmann

Asked Jun 20, 2007, 11:47 PM EDT3 answers
Share
LinkedInX
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.

  • 0
    I dont know if you can do that with a listbox. To do so would be a dynamic interaction with a usually static object.
    I could be wrong and someone please correct me if I am, but I dont think it can be done without some serious coding.
    0
  • Mike Gold
    0
    This ought to get you started.  This is a custom ListBox control I created that allows you to drag items around in the listbox.  You'll need to set property AllowDrop to true and wire all the events, but this code should give you the idea. Perhaps I'll write an article describing the full process

    Best,

    -Mike

    C#
    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);
    }
    }
    }
    0
  • 0
    Thanks mike, exactly what I was looking for :) FYI, there are some other solutions floating around on the web, notably a listview implementation as well as another with a move up/move down button. I'll try and find the links and post them here. http://www.codeproject.com/cs/combobox/orderedlistbox.asp (seems a little buggy) also worth looking at is http://www.codeproject.com/cs/combobox/LarryDragAndDrop.asp
    0