Solved

C# - Extended drag and drop functionality

Posted on 2007-04-05
11
2,753 Views
Last Modified: 2008-09-08
Hi guys 'n gals,

I have, from a previous question a few days ago, discovered how to handle a drag and drop event. I now need some help with taking that further...


I have a load of PictureBox's inside a Panel (panel2)
I want to create a function, which will allow me to drag an instance/copy of one of these PictureBox's from their Panel (panel2), and be able to drop the clones into another Panel (panel1).

So basically, I am trying to achieve a feature much like found in Visio, where you can drag and drop shapes from the Catalog onto the Drawing area.


Anybody able to either point me in the right direction, or to help me out?


Cheers!
0
Comment
Question by:Cyber-Drugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18858872
Just think that Drag/Drop is copy/paste operation. It is good way to realize copy/paaste first.
You can copy the full picture in clipboard or only path to picture.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18858898
The thing is, I know how to actually duplicate the image, the problem I am having understanding is how to allow is to onclick for the original image, a clone is created above the original (outside of the Panel so free movement is possible), and the currently selected object while mouse is down is moved from the original image to the newly created clone, so that while the mouse starts to move, the clone is dragged rather than the original. I can handle the rest, no problem.



Cheers!
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18858988
Aah, you need only visualization. Try to hide original image when you start to move clone.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18859538
Hi again Cyber-Drugs,

Create a Form.  Add Panel1 on the Left and Panel2 on the Right.  Add several PictureBoxes to Panel1 and set their Image Properties to something.

Using the code below, drag a "tool" from Panel1 to Panel2...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class VisioStyleDragDropForm : Form
    {
        public VisioStyleDragDropForm()
        {
            InitializeComponent();
        }

        PictureBox pb;
        int startX, startY;

        private void VisioStyleDragDropForm_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in panel1.Controls)
            {
                if (ctl is PictureBox)
                {
                    ctl.MouseDown += new MouseEventHandler(ctl_MouseDown);
                    ctl.MouseMove += new MouseEventHandler(ctl_MouseMove);
                    ctl.MouseUp += new MouseEventHandler(ctl_MouseUp);
                }
            }
        }

        void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                startX = e.X;
                startY = e.Y;
                PictureBox source = (PictureBox)sender;
                pb = new PictureBox();
                pb.Size = source.Size;
                pb.Location = this.PointToClient(panel1.PointToScreen(source.Location));
                pb.Image = source.Image;
                this.Controls.Add(pb);
                pb.BringToFront();
            }
        }

        void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pb.Location = new Point(pb.Location.X + (e.X - startX), pb.Location.Y + (e.Y - startY));
                startX = e.X;
                startY = e.Y;
            }
        }

        void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pb.ClientRectangle.IntersectsWith(panel2.ClientRectangle))
                {
                    pb.Hide();
                    pb.Location = panel2.PointToClient(this.PointToScreen(pb.Location));
                    panel2.Controls.Add(pb);
                    pb.Show();
                }
                else
                {
                    pb.Dispose();
                }
                pb = null;
            }
        }

    }
}
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18859808
2 Idle_Mind
Could it work between application?

2 Cyber-Drugs
How about his sample?
http://www.codeproject.com/csharp/dandtutorial.asp
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18859883
Not in it's current state no.  This is a specific pseudo "drag and drop" functionality between two panels in the same app.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18860302
Well Idle_Mind, seems once again you solved something I thought to be extremely complex with a very easy to follow bit of code, thank you!!!

Works perfectly!! I just need to now add some extra bits to suite my own needs, cheers!!
0
 
LVL 1

Expert Comment

by:sayeth
ID: 18866455
very good idle_mind,but can you create colision detection in panel2  with the picturebox
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18866479
Hi sayeth,

Could you be a little more specific?  What is the behaviour you are looking for?
0
 
LVL 1

Expert Comment

by:sayeth
ID: 18869404
in panel2; an arrayList or some object to keep track of the image and it's position. if the user drop an
image on top of another image, the is a messegeBox that shows."there is a preexisting image in the
desire location, do you wich to delete the preexisting image; Yes or No". if yes, the old image is deleted
and the new image is place at that location. if No, the old image remains. you can't put a pictureBox on
the top of another in the drop area.
0
 
LVL 1

Expert Comment

by:sayeth
ID: 18908280
this program only drag and drop, i want it to also do collision detection.

I want the program to  also do this ( in panel1; an arrayList or some object to keep track of the image and it's position. if the user drop an image on top of another image, the is a messegeBox that shows."there is a preexisting image in the desire location, do you wich to delete the preexisting image; Yes or No". if yes, the old image is deleted and the new image is place at that location. if No, the old image remains. you can't put a pictureBox on the top of another in the drop area)

do think this code on the will work

private bool PictureBoxHitTest(PictureBox pb1)
        {
            foreach (Control ctl in panel2.Controls)
            {
                if (ctl.GetType() == typeof(PictureBox))
                {
                    pb2 = (PictureBox)ctl;
                    if (pb.ClientRectangle.IntersectsWith(pb2.ClientRectangle))
                        return true;
                }
            }
            return false;
        }
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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