FlowLayoutPanel Drag and Drop实现拖拽排序
说明:FlowLayoutPanel里边放入Label,对Label拖拽实现排序。
1.给Label注册MouseDown事件:
label.MouseDown += new System.Windows.Forms.MouseEventHandler(Label_MouseDown);
响应Label鼠标按下事件内容:11
private void Label_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && e.Clicks == 1) { base.OnMouseDown(e); Label label = sender as Label; label.DoDragDrop(label, DragDropEffects.All); } }
说明:上边过滤了鼠标左键单击
2.设置FlowLayoutPanel.AllowDrop = true;
3.给FlowLayoutPanel注册DragEnter
和DragDrop
事件,响应代码如下:
private void FlowLayoutPanel_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; } private void FlowLayoutPanel_DragDrop(object sender, DragEventArgs e) { Label label = (Label)e.Data.GetData(typeof(Label)); Point point = FlowLayoutPanel.PointToClient(new Point(e.X, e.Y)); Control control = FlowLayoutPanel.GetChildAtPoint(point); if (control != null && label != control) { IsXuHaoChange = true; int index = FlowLayoutPanel.Controls.GetChildIndex(control, false); FlowLayoutPanel.Controls.SetChildIndex(label, index); FlowLayoutPanel.Invalidate(); } }
这样,FlowLayoutPanel里边的Label子控件即可通过拖拽排序,排序方法是把拖拽的Label拖拽到要替换的Label上,其他Label完后移位。
最近评论