?
Solved

C# - Extended drag and drop functionality

Posted on 2007-04-05
11
Medium Priority
?
2,755 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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