【问题标题】:DragDrop between controls在控件之间拖放
【发布时间】:2012-02-09 07:28:34
【问题描述】:

我对 DragDrop 有疑问。

    private void Form0_Load(object sender, EventArgs e)
    {
        PictureBox panel1 = new PictureBox();
        PictureBox panel2 = new PictureBox();

        mainPanel.Dock = DockStyle.Fill;
        this.Controls.Add(mainPanel);

        panel1.Location = new Point(10, 10);
        panel1.Size = new System.Drawing.Size(500, 300);
        panel1.BorderStyle = BorderStyle.FixedSingle;

        Button b2 = new Button();
        b2.Location = new Point(10, 10);
        panel2.Controls.Add(b2);
        panel2.Location = new Point(10, 10);
        panel2.Size = new System.Drawing.Size(200, 100);
        panel2.BorderStyle = BorderStyle.FixedSingle;

        foreach (Control c in panel1.Controls)
        {
            c.MouseDown += new MouseEventHandler(control_MouseDown);
            c.MouseMove += new MouseEventHandler(control_MouseMove);
            c.MouseUp += new MouseEventHandler(control_MouseUp);
            c.AllowDrop = true;
        }
        panel1.AllowDrop = true;

        panel1.DragEnter += new DragEventHandler(container_DragEnter);
        panel1.DragDrop += new DragEventHandler(container_DragDrop);
        panel1.DragOver += new DragEventHandler(container_DragOver);

        foreach (Control c in panel2.Controls)
        {
            c.MouseDown += new MouseEventHandler(control_MouseDown);
            c.MouseMove += new MouseEventHandler(control_MouseMove);
            c.MouseUp += new MouseEventHandler(control_MouseUp);
            c.AllowDrop = true;
        }
        panel2.AllowDrop = true;

        panel2.DragEnter += new DragEventHandler(container_DragEnter);
        panel2.DragDrop += new DragEventHandler(container_DragDrop);
        panel2.DragOver += new DragEventHandler(container_DragOver);

        mainPanel.Controls.Add(panel1);
        mainPanel.Controls.Add(panel2);
        mainPanel.Controls.Add(pb);
    }

    private void control_MouseDown(object sender, MouseEventArgs e)
    {
        Control c = sender as Control;
        isDragging = true;
        clickOffsetX = e.X;
        clickOffsetY = e.Y;
    }

    private void control_MouseMove(object sender, MouseEventArgs e)
    {
        Control c = sender as Control;
        if (isDragging == true)
        {
            c.Left = e.X + c.Left - clickOffsetX;
            c.Top = e.Y + c.Top - clickOffsetY;
            if (c.Location.X + clickOffsetX > c.Parent.Width ||
                c.Location.Y + clickOffsetY > c.Parent.Height ||
                c.Location.X + clickOffsetX < 0 ||
                c.Location.Y + clickOffsetY < 0)

                c.DoDragDrop(c, DragDropEffects.Move);
        }
    }

    private void control_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

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

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

        //if (e.Data.GetDataPresent(typeof(Bitmap)))
        //{
        //    e.Effect = DragDropEffects.Copy;
        //}
        //else
        //{
        //    e.Effect = DragDropEffects.None;
        //}
    }

    private void container_DragDrop(object sender, DragEventArgs e)
    {
        Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
        PictureBox p = sender as PictureBox;
        mycontrol = c;
        isDragging = false;
        if (c != null)
        {
            c.Location = p.PointToClient(new Point(e.X, e.Y));
            p.Controls.Add(c);
        }
    }

这是一个工作示例。但我不能将控件从父控件拖放到子控件。什么是魔法?如何将控件拖放到另一个控件(在我的示例中从 panel1 到 panel2)。

【问题讨论】:

  • @justAuser:你到底想做什么?你想在哪个控制上放弃另一个控制?你能解释一下这个场景吗..
  • @Smack:问题是我怎样才能把孩子丢给同一个父母的另一个孩子?
  • 您需要重新设置控件的父级。更改Parent 属性。我不知道你在寻找什么答案,这个问题还没有变得更清楚。这只是一堵巨大的 [工作] 代码墙。

标签: c# drag-and-drop controls containers


【解决方案1】:

这里有一些答案,可能会对你有所帮助:

看到这个Move controls when Drag and drop on panel in C#

这是有关如何托管表单设计器的完整示例:

Tailor Your Application by Building a Custom Forms Designer with .NET

也检查这个简单的标签拖放: Basic drag and drop in WinForms

【讨论】:

  • 我以前见过这些例子。没事。但是,在我的情况下,没有任何解决方案会有所帮助。问题是如何将孩子转移到同一父母的另一个孩子?
  • 发送您正在拖动的控件 ID,并将其从父级中删除并添加到您要放置的子面板中。您可以在父级中找到具有 id 的控件..
猜你喜欢
  • 2023-03-04
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
相关资源
最近更新 更多