Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, trying to implement drag and drop
I have custom ToolBox class that simply iherits FlowLayoutPanel, and some picture boxes within. When user clicks on any of these picture boxes, it got copied and added to form's controls just on top of original. Then when user moves mouse around with left button pressed, that picture box should move along, but it don't. It works only if you release mouse button and then click again and move.


What I have tried:

C#
//want to mentioned that that's not the full code, only what is related to the question
 public partial class FigureToolBox : FlowLayoutPanel
 {

        public delegate void FigureSelected(PictureBox figurePicture,Point location);

        public event FigureSelected OnFigureSelect;
       
      //on mouse down of each picture box
      public void Figure_mouse_down(object c, MouseEventArgs e)
        {
            PictureBox pictureBox = c as PictureBox;

            PictureBox clonedPicture = new PictureBox();
            clonedPicture.SizeMode = PictureBoxSizeMode.AutoSize;
            clonedPicture.Image = pictureBox.Image;
            
            OnFigureSelect(clonedPicture, e.Location);

        }

     //some code
}
public partial class GameForm : Form
{
   Point point = new Point(); //start point of moving
   PictureBox draggedFig;


   public GameForm()
   {
     InitializeComponent();
     figureToolBox1.OnFigureSelect += FigureSelected;
   }

   private void FigureSelected(PictureBox box, Point location)
   {
            draggedFig = box;
            //just calculating  location of picture box in form's coords
            Point point = PointToClient(Cursor.Position);
            box.Left = (point.X- location.X);
            box.Top = (point.Y- location.Y);

            this.point = location;
            
            this.Controls.Add(draggedFig);
            draggedFig.BringToFront();

            draggedFig.MouseMove += DraggedFig_Move;
            draggedFig.Focus(); //thought it will help, but it won't
     }

    private void DraggedFig_Move(object sender, MouseEventArgs e){

            if (e.Button == MouseButtons.Left)
            {
                draggedFig.Left += e.X - point.X;
                draggedFig.Top += e.Y - point.Y;
            }
     }
}
Posted
Updated 23-Apr-19 8:48am
Comments
Member 14015063 22-Apr-19 12:40pm    
Finally got it. Just needed to disable toolbox before firing OnFigureSelect:
...
this.Enabled = false;
OnFigureSelect(clonedPicture, e.Location);

And then enable it back when mouse button released.

See where the "focus" is. If you can set the focus to the new PictureBox (which is probably what the second click does) it might work.
 
Share this answer
 
Comments
Member 14015063 22-Apr-19 10:38am    
Well, i tried, but it don't work. There's the line: draggedFig.Focus();
Or is there another way to focus on control?
[no name] 22-Apr-19 10:42am    
You need to "trace" ALL the events. If setting it at one point doesn't work, it may at another. You have a particular scenario YOU need to understand in terms of what's going on, what behavior to "skip", etc. There is no "one size fits all". It's a question of FOCUS. Deal with it.
 
Share this answer
 
v2
First, be aware you are not using the WinForms Drag/Drop facility that has some sophisticated features for giving feedback while dragging, etc.

See if you get some ideas from this example, which like yours, is using a simple method
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ToolBoxPanel : UserControl
    {
        public ToolBoxPanel()
        {
            InitializeComponent();
        }


        private void ToolBoxPanel_Load(object sender, EventArgs e)
        {
            parent = this.Parent as Form;
        }

        private PictureBox currentPB;

        private bool IsMouseUp = true;

        private CheckBox cb;

        private int cbCount = 0;

        private Form parent;

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            currentPB = sender as PictureBox;

            switch (currentPB.Name)
            {
                case "pictureBox1":

                    cb = new CheckBox();

                    parent.Controls.Add(cb);
                    cb.Text = "CheckBox " + cbCount++;
                    cb.Show();
                    cb.BringToFront();

                    break;
            }

            IsMouseUp = false;
        }

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseUp = true;

            // cancel the drop if it's not over the Form ?
            if(this.DisplayRectangle.Contains
              (this.PointToClient
                (currentPB.PointToScreen(e.Location))))
            {
                parent.Controls.Remove(cb);
                cb.Dispose();
            }
        }

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseUp) return;

            // map the mouse location to the Parent Form
            cb.Location = parent.PointToClient(this.PointToScreen(e.Location));
        }
    }
}
Note: the moved Control in this example is not movable once it has been sited on the parent Form.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900