Solved

C# - Extended drag and drop functionality

Posted on 2007-04-05
11
2,741 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
  • 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
 
LVL 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
coded character set iptc field within a jpeg 1 70
Why Does This Page Not Show Up? 4 24
free scanner TWAIN can be operated with a Web application 9 40
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now