Solved

C# - Extended drag and drop functionality

Posted on 2007-04-05
11
2,745 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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

8 Experts available now in Live!

Get 1:1 Help Now