2013-1224

FlowLayoutPanel Drag and Drop实现拖拽排序

作者: momy 分类: 编程开发 2 Comment »
摘要: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注册DragEnterDragDrop事件,响应代码如下:


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完后移位。

标签: FlowLayoutPanel Drag and Drop 拖拽排序 阅读: 6204
上一篇: IIS7移除响应头里边的服务器信息 - 7032次
下一篇: 关于程序员的59条搞笑但却真实无比的编程语录 - 3995次

网友评论

183.135.117.* 2015/3/21 13:36:05

有完整的代码吗

36.110.120.* 2016/12/10 17:28:57

非常好,终于找到可以用的了

向右滑动解锁留言