拖放CustomControl只能从它的后台运行

时间:2014-05-08 07:35:40

标签: c# drag-and-drop custom-controls draggable flowlayoutpanel

我有一个自定义控件,其中包含一个带有标签的按钮。我想让这些控件可以在另一个控件上拖动,以便在flowlayoutpanel中按照我的要求进行排列。 现在只有当我将控件从它的背景(在下面的图片中用黄色标记)拖动到另一个控件的黄色标记区域时才起作用,但是如果我从按钮或标签区域拖动则不行。

enter image description here

如何制作它以便我可以移动自定义控件,无论我从何处抓取并将其放在另一个控件上。基本上只有一个控件不像按钮和标签的容器..

到目前为止,这是我的代码:

 private void flowLayoutPanel1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
    {         
        CustomControl target = sender as CustomControl;
        if (target != null)
        {
            int targetIndex = FindCSTIndex(target);
            if (targetIndex != -1)
            {
                string pictureBoxFormat = typeof(CustomControl).FullName;
                if (e.Data.GetDataPresent(pictureBoxFormat))
                {
                    CustomControl source = e.Data.GetData(pictureBoxFormat) as CustomControl;

                    int sourceIndex = this.FindCSTIndex(source);

                    if (targetIndex != -1)
                        this.flowLayoutPanel1.Controls.SetChildIndex(source, targetIndex);
                }
            }
        }
    }

    private int FindCSTIndex(CustomControl cst_ctr)
    {
        for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
        {    
            CustomControl target = this.flowLayoutPanel1.Controls[i] as CustomControl;

            if (cst_ctr == target)
                return i;
        }
        return -1;
    }

    private void OnCstMouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            CustomControl cst = sender as CustomControl;
            cst.DoDragDrop(cst, DragDropEffects.Move);
        }
    }

自定义控件类:

public class CustomControl : Control
{
    private Button _button;
    private Label _label;

    public CustomControl(Button button, Label label)
    {
        _button = button;
        _label = label;
        button.Width = 50;
        button.Height = 50;
        label.Width = 65;
        button.BackgroundImageLayout = ImageLayout.Stretch;
        Height = button.Height + label.Height;
        Width = 68;

        // Width = Math.Max(button.Width, label.Width);
        Controls.Add(_button);
        _button.Location = new Point(0, 0);
        Controls.Add(_label);
        _label.Location = new Point(0, button.Height);
    }
}

2 个答案:

答案 0 :(得分:1)

使用MouseDown代替MouseMove来启动拖放(MSDN)。你可以在控制代码本身中启动拖放(假设所有CustomControl都可以拖放),否则你可能想要创建公共方法来签署孩子(暴露孩子是坏的)想法,除非你已经在外面使用它们。)

public class CustomControl : Control
{
    ...

    public CustomControl(Button button, Label label)
    {
        ...

        _button.MouseDown += OnMouseDown;
        _label.MouseDown += OnMouseDown;
    }

    override void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            (sender as Control).DoDragDrop(this, DragDropEffects.Move);
    }
}

未经测试,但应该给你一些想法。

答案 1 :(得分:0)

管理解决它:

private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
    {
        Control target = new Control();

        target.Parent = sender as Control;

            if (target != null)
            {
                int targetIndex = FindCSTIndex(target.Parent);
                if (targetIndex != -1)
                {
                    string cst_ctrl = typeof(CustomControl).FullName;
                    if (e.Data.GetDataPresent(cst_ctrl))

                    {
                        Button source = new Button();
                        source.Parent = e.Data.GetData(cst_ctrl) as CustomControl;

                        if (targetIndex != -1)
                            this.flowLayoutPanel1.Controls.SetChildIndex(source.Parent, targetIndex);
                    }
                }
            }
        }

    private int FindCSTIndex(Control cst_ctr)
    {
        for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
        {    
            CustomControl target = this.flowLayoutPanel1.Controls[i] as CustomControl;

            if (cst_ctr.Parent == target)
                return i;
        }
        return -1;
    }

    private void OnCstMouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Control cst = sender as Control;
            cst.DoDragDrop(cst.Parent, DragDropEffects.Move);
        }
    }