Solved

C# - Extended drag and drop functionality

Posted on 2007-04-05
11
2,737 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 48
cs.Designer Issue 2 20
Calling stored proc in EDMX 11 19
C# TextBox 11 16
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

10 Experts available now in Live!

Get 1:1 Help Now