Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: is there a Click-and-drag "Desktop-Like" control?

OK, first for context look at the Windows desktop; You can take items (folders, files) on the desktop and drag them around to different places and they "stay" where you dragged them. This seems to be a pretty useful feature to offer users so as to allow them to create their own "groupings" of items.

My question is thus: Is there a control in .NET that approximates this behavior with a collection of items?

I'm thinking something like a listview in "LargeIcon" mode, but it allows you to drag the icons around to different places inside the control.

like image 513
BKimmel Avatar asked Jan 21 '26 21:01

BKimmel


2 Answers

You can do this with a standard ListView control by implementing drag-and-drop. Here's a sample control that does this:

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyListView : ListView {
  private Point mItemStartPos;
  private Point mMouseStartPos;

  public MyListView() {
    this.AllowDrop = true;
    this.View = View.LargeIcon;
    this.AutoArrange = false;
    this.DoubleBuffered = true;
  }

  protected override void OnDragEnter(DragEventArgs e) {
    if (e.Data.GetData(typeof(ListViewItem)) != null) e.Effect = DragDropEffects.Move;
  }
  protected override void OnItemDrag(ItemDragEventArgs e) {
    // Start dragging
    ListViewItem item = e.Item as ListViewItem;
    mItemStartPos = item.Position;
    mMouseStartPos = Control.MousePosition;
    this.DoDragDrop(item, DragDropEffects.Move);
  }
  protected override void OnDragOver(DragEventArgs e) {
    // Move icon
    ListViewItem item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
    if (item != null) {
      Point mousePos = Control.MousePosition;
      item.Position = new Point(mItemStartPos.X + mousePos.X - mMouseStartPos.X,
          mItemStartPos.Y + mousePos.Y - mMouseStartPos.Y);
    }
  }
}
like image 76
Hans Passant Avatar answered Jan 24 '26 13:01

Hans Passant


I think the closest would the ListView control, but even that is more like an explorer window. You might be able to create your own view that does what you want, but you'd need to manually persist icon locations somewhere.

like image 24
Joel Coehoorn Avatar answered Jan 24 '26 11:01

Joel Coehoorn