Dragging a form onto another in an MDI environment in C#

Hi all,

Is it possible to detect when one Child form is dragged over a specific location of another child form in an MDI application? (Kind of drag-drop an entire form into another)

Any help would be appreciated.
Best regards,
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...use the LocationChanged() event of the form.  You can compare positions by converting the child form point coords to screen coords with PointToScreen().
James AtkinSenior Principle Software EngineerAuthor Commented:
Do you have any examples of the usage of this?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Really not sure what your setup is so here's a contrived example.  I have a main MdiParent form (Form1) and two MdiChildren (MdiChildA, MdiChildB).  In MdiChildB I placed a PictureBox (pictureBox1), made its border visible, and changed its Modifiers() Property to True so that the PictureBox would be visible outside the form class and accessible from the MdiParent.  I made MdiChildA smaller than the PictureBox in MdiChildB.

When the MdiParent opens the two children are displayed.  When MdiChildA is dragged completely into the PictureBox on MdiChildB you should get a MessageBox.  You could also check if the Cursor position is within the PictureBox or use the Intersects() method of the Rectangle to check for a hit instead of complete containment:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
    public partial class Form1 : Form

        private MdiChildA frmA;
        private MdiChildB frmB;

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            frmA = new MdiChildA();
            frmA.MdiParent = this;
            frmA.LocationChanged += new EventHandler(frmA_LocationChanged);

            frmB = new MdiChildB();
            frmB.MdiParent = this;

        void frmA_LocationChanged(object sender, EventArgs e)
            Rectangle rc = frmB.pictureBox1.RectangleToScreen(frmB.pictureBox1.ClientRectangle);
            if (rc.Contains(this.RectangleToScreen(frmA.Bounds)))
                MessageBox.Show("frmA dropped into frmB's pictureBox1");


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James AtkinSenior Principle Software EngineerAuthor Commented:
That works great! Thanks :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.