-1

I apologize in advance if this has been asked already. Searching stackoverflow and the internet didn't present any helpful examples.

In the running program, I have a picture box I need to be able to click and drag from one GroupBox to another. I know how to drag the picture box around the WinForm itself (i.e. without any GroupBoxes involved). I can't find any examples of how to do this.

I've created code for every event it seems would be relevant to group box / mouse interaction (see end of post for code).

Note: Thanks for the feedback on needing the code. I'm new Stack Overflow.

INTERFACE: Initial Interface

PROBLEM: When I click and drag pbxMoveIt over the group boxes, the groupbox events never fire, based on the text in the status label.

Dragging image over group box doesn't trigger group box enter, hover or other events

When I move the mouse without dragging anything, the group box mouse hover event fires.

Group box mouse hover event fires when not dragging anything.

ADDED Code:

namespace MoveControlsOnFormBetweenGroupBoxes {
    public partial class frmMain : Form {

        private Point m_MouseDownLocation;
        private bool m_IsDragging;

        public frmMain ( ) {
            InitializeComponent ( );

            pbxMoveIt.BringToFront ( );
            gbx1.AllowDrop = true;
            gbx2.AllowDrop = true;
            lblStatus.Text = "GUI Status: Started";
        }

        #region Picture Box Related Methods
        // Picture Related Methods
        private void pbxMoveIt_MouseDown ( object sender, MouseEventArgs e ) {
            lblStatus.Text = "GUI Status: pbxMoveIt - MouseDown";
            if ( e.Button == MouseButtons.Left ) {
                m_MouseDownLocation = e.Location;
                m_IsDragging = true;
            }
        }
        private void pbxMoveIt_MouseMove ( object sender, MouseEventArgs e ) {
            int newX;
            int newY;
            int minX = 10;
            int minY = 10;
            int maxX = this.Width - (25 + pbxMoveIt.Width);
            int maxY = this.Height - (45 + pbxMoveIt.Height);
            if ( e.Button == MouseButtons.Left ) {
                lblStatus.Text = "GUI Status: pbxMoveIt - MouseMove";
                newX = e.X + pbxMoveIt.Left - m_MouseDownLocation.X;
                newY = e.Y + pbxMoveIt.Top - m_MouseDownLocation.Y;
                if ( m_IsDragging ) {
                    if ( ( newX >= minX ) && ( newX <= maxX ) ) {
                        pbxMoveIt.Left = newX;
                    }
                    if ( ( newY >= minY ) && ( newY <= maxY ) ) {
                        pbxMoveIt.Top = newY;
                    }
                }
            }
        }
        private void pbxMoveIt_MouseUp ( object sender, MouseEventArgs e ) {
            lblStatus.Text = "GUI Status: pbxMoveIt - MouseUp";
            if ( e.Button == System.Windows.Forms.MouseButtons.Left ) {
                m_IsDragging = false;
            }
        }
        #endregion

        #region Group Box Related
        // Group Box Related Methods
        private string Gbx_Title ( object sender ) {
            string boxTitle = "Unknown";
            if ( sender == gbx1 ) {
                boxTitle = "Group Box 1";
            }
            if ( sender == gbx2 ) {
                boxTitle = "Group Box 2";
            }
            return boxTitle;
        }
        private void gbx_DragDrop ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragDrop", Gbx_Title ( sender ) );
        }
        private void gbx_DragEnter ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragEnter", Gbx_Title ( sender ) );
        }
        private void gbx_DragLeave ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragLeave", Gbx_Title ( sender ) );
        }

        private void gbx_DragOver ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragOver", Gbx_Title ( sender ) );
        }

        private void gbx_Enter ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - Enter", Gbx_Title ( sender ) );
        }

        private void gbx_Leave ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - Leave", Gbx_Title ( sender ) );
        }

        private void gbx_MouseHover ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - MouseHover", Gbx_Title ( sender ) );
        }
        #endregion
    }
}
  • Do you actually drag & drop ie use the DragDrop/Enrter etc events? If so it shouldn't be hard to code the events for the 2nd groupbox. Show the code you tried.. Note that the drop action will involve adding the pbox to the gbox's Controls collection plus setting a suitable location there! – TaW Mar 06 '18 at 09:13
  • See [How to drag and drop a button from one panel to another panel?](https://stackoverflow.com/q/11407068/719186) – LarsTech Mar 06 '18 at 22:03
  • @TaW: I do have the group box events defined. Right now they just change a update label text on the main form. When I know how to get the events to fire, I'll be able to put the other functionality I want in. – Jess Stuart Mar 06 '18 at 22:09
  • You aren't setting the effect in the DragEnter event. See the example I linked. I also don't see you calling DoDragDrop. – LarsTech Mar 06 '18 at 22:09
  • @LarsTech: I based my code, which isn't working the way I need, on that example. I just put the code in (sorry, I should have done that sooner). – Jess Stuart Mar 06 '18 at 22:10
  • It looks like you are trying to create your own drag-drop code. In which case, you would have to use MouseEnter, MouseUp, MouseLeave, etc. But there's little reason to reinvent the wheel. – LarsTech Mar 06 '18 at 22:12
  • @LarsTech: Q1: There is a DragEnter event defined for the group boxes. Do you mean something is missing from it? – Jess Stuart Mar 06 '18 at 22:13
  • @LarsTech: Q2: What is this DoDragDrop of which you type? – Jess Stuart Mar 06 '18 at 22:13
  • @LarsTech: Q3: The groupbox objects only have MouseHover defined, not MouseEnter, MouseUp, nor MouseLeave. – Jess Stuart Mar 06 '18 at 22:16
  • MouseEnter is hidden in the designer. You would have to attach the event in code. Just look at my example code and test it out in a new project. – LarsTech Mar 06 '18 at 22:19
  • That worked .... almost. The picturebox stays in each group box as I want when I drag/drop it there. Is there any way to keep the image in the picturebox from disappearing when I'm dragging it? – Jess Stuart Mar 06 '18 at 23:25

1 Answers1

0

Add my class to your project

using System;
using System.Windows.Forms;

namespace Hector.Framework.Utils
{
    public class Drag : Form
    {
        private bool isDraggable;
        private Control target;
        private int a, b;

        public Drag(){}

        public void Grab(Control control)
        {
            try
            {
                this.isDraggable = true;
                this.target = control;
                this.a = Control.MousePosition.X - this.target.Left;
                this.b = Control.MousePosition.Y - this.target.Top;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void MoveObject(bool Horizontal = true, bool Vertical = true)
        {
            try
            {
                if (this.isDraggable)
                {
                    int x = Control.MousePosition.X,
                        y = Control.MousePosition.Y;

                    if (Horizontal)
                    {
                        this.target.Left = x - this.a;
                    }
                    if (Vertical)
                    {
                        this.target.Top = y - this.b;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void Release()
        {
            this.isDraggable = false;
        }
    }
}

Then, call PictureBox Events

    public partial class Form1 : Form
    {
    private Hector.Framework.Utils.Drag drag = new Hector.Framework.Utils.Drag();

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
       drag.Grab(pictureBox);
    }

    private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
       drag.Release();
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
       drag.MoveObject();
    }
    }
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • Unfortunately, this Drag class reproduces the issue where the groupbox events don't fire. What I need is for the mouse in drag mode to look like the image in the picture box. That's a different question, though. – Jess Stuart Mar 07 '18 at 06:26