private void lstRead_MouseDown(object sender, MouseEventArgs e)
{
if (this.lstRead.SelectedItem == null)
{
return;
}
//开始拖放操作,DragDropEffects为枚举类型。
//DragDropEffects.Move 为将源数据移动到目标数据
this.lstRead.DoDragDrop(this.lstRead.SelectedItem, DragDropEffects.Move);
}
private void lstRead_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void lstRead_DragDrop(object sender, DragEventArgs e)
{
Point point = lstRead.PointToClient(new Point(e.X, e.Y));
int index = this.lstRead.IndexFromPoint(point);
if (index < 0)
{
index = this.lstRead.Items.Count - 1;
}
//获取拖放的数据内容
object data = e.Data.GetData(typeof(string));
//删除原数据
this.lstRead.Items.Remove(data);
//插入目标数据
this.lstRead.Items.Insert(index, data);
}
C#Winform 实现ListBox中项的拖拽
本文转载:CSDN博客