[Webinar] Streamline your web hosting managementRegister Today


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

Posted on 2010-04-01
Medium Priority
Last Modified: 2012-06-27
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,
Question by:James Atkin
  • 2
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 29380047
Sure...use the LocationChanged() event of the form.  You can compare positions by converting the child form point coords to screen coords with PointToScreen().

Author Comment

by:James Atkin
ID: 29906690
Do you have any examples of the usage of this?
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 29920692
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


Author Closing Comment

by:James Atkin
ID: 31710091
That works great! Thanks :-)

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question