【问题标题】:C# drag drop reselecting a picturebox to mve from formC# 拖放选择一个图片框以从表单中移动
【发布时间】:2013-11-06 05:12:24
【问题描述】:

所以我有一个项目,我需要拖放不同的图片框,并在我拖放它们时将它们的现有副本制作到表单中。我的问题是,在表单上创建选定的“图片框”后,我无法移动它。我想有一个选项,我可以移动任何被拖动的图片框,而不是像在某个位置拖动并将其保持在该位置。

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {


        if (e.Button == MouseButtons.Left)
        {
            p = (PictureBox)sender;
            p.Tag = p.Location;
            downPoint = e.Location;
            p.Parent = this;
            p.BringToFront();

        }

    }
    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        p = (PictureBox)sender;
        if (e.Button == MouseButtons.Left)
        {
            p.Left += e.X - downPoint.X ;
            p.Top += e.Y - downPoint.Y ;


        }
    }
     private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        p = (PictureBox)sender;

        PictureBox PB = new PictureBox();


        Control c = GetChildAtPoint(new Point(p.Left -1, p.Top));
        if (c == null) c = this;
        Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
        PB.Parent = c;
        PB.Location = newLoc;


       ;

        p.Parent.Controls.Add(PB); // <-- add new PB to the form!
        p.Location = (Point)p.Tag;
        // put the original back where it started:

    }

【问题讨论】:

    标签: c# drag-and-drop picturebox


    【解决方案1】:

    将事件放入新的图片框

    PB.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
    PB.MouseDown += new MouseEventHandler(picturebOX_MouseDown);
    PB.MouseUp += new MouseEventHandler(picturebOX_MouseUp);
    

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2015-04-08
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多