Skip to content

Instantly share code, notes, and snippets.

@rekkusu
Created March 11, 2010 11:58
Show Gist options
  • Save rekkusu/329059 to your computer and use it in GitHub Desktop.
Save rekkusu/329059 to your computer and use it in GitHub Desktop.
// コード汚いです。。
// import System.Windows.Forms
public class SortableListBox : ListBox
{
int moveItemIndex = -1;
public event EventHandler ItemSorted;
public SortableListBox()
: base()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
moveItemIndex = this.IndexFromPoint(e.Location);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (moveItemIndex >= 0)
{
int current = this.IndexFromPoint(e.Location);
if (moveItemIndex != current && current >= 0)
{
object item = this.Items[moveItemIndex];
this.Items.RemoveAt(moveItemIndex);
this.Items.Insert(current, item);
moveItemIndex = current;
this.SelectedIndex = current;
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
moveItemIndex = -1;
this.OnItemSorted(EventArgs.Empty);
}
}
protected virtual void OnItemSorted(EventArgs e)
{
if (ItemSorted != null) ItemSorted(this, EventArgs.Empty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment