Created
March 11, 2010 11:58
-
-
Save rekkusu/329059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// コード汚いです。。 | |
// 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