• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

Passing PictureBox in Drag and Drop

Hello experts :)

I have PictureBox with selected image and selected Tag- tag is just integer.

I want to pass these two values to another PictureBox using drag and drop.

How can I do it?

Thx

panJacek


0
panJacek
Asked:
panJacek
  • 6
  • 3
  • 3
2 Solutions
 
anarki_jimbelCommented:
Dragging image from one picturebox to another should be pretty simple:

http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-enable-dragging-and-dropping-an-image-from-one-picturebox-to-another.aspx

However you need to drag and drop also tag info. I believe you need to create a new class that will hold image and tag info. I.e the class will need to have two properties: image (bitmap) and integer(tag info). When initiating drag-drop operation you create an instance of the class and "drag" it (not just an image as in the above example). When releasing the button you get the instance and extract image and tag info from it and set for the target picture box.
0
 
panJacekAuthor Commented:
anarki_jimbel::

but why should I create new class when PictureBox already have both Image and Tag...

0
 
anarki_jimbelCommented:
Because you are not dragging PictureBox itself - you are drugging image (image info).

Really you may use PictureBox class but it has too many properties we don't need for "dragging" the information.
0
Technology Partners: 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!

 
panJacekAuthor Commented:
I do not get it, if I cannot drag PictureBox, how can I drag my own class?

0
 
anarki_jimbelCommented:
You may drag any object. The problem is that all examples I found are for strings or images.
OK, try my snippet. Create a new class as I told and add drag'n'drop code to the form. It's very similar to the example I referred above.

You may delete output code later. I used it to check that the tag in the second picturebox changes. Statements print to the output window.
    public class ImageWithTag
    {
        private System.Drawing.Image _img ;
        private object _tag;

        public System.Drawing.Image Img
        {
          get { return _img; }
          set { _img = value; }
        }

        public object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }
    }

//==========================================================
        private void Form1_Load(object sender, EventArgs e)
        {
            // Set AllowDrop of the Target PictureBox to true
            //   as this property cannot be set in the Designer
            pictureBox2.AllowDrop = true;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ImageWithTag it = new ImageWithTag();
                it.Img = pictureBox1.Image;
                it.Tag = pictureBox1.Tag;
                pictureBox1.DoDragDrop(it, DragDropEffects.All);
            }
                
        }

        private void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            bool present = e.Data.GetDataPresent(typeof (ImageWithTag));//
            System.Diagnostics.Debug.Write("=== present = " + present);
            e.Effect = present ? DragDropEffects.Copy : DragDropEffects.None;

        }

        private void pictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ImageWithTag)))
            {
                pictureBox2.Image = ((ImageWithTag)e.Data.GetData(typeof(ImageWithTag))).Img;//DataFormats.GetFormat("ImageWithTag")
                pictureBox2.Tag = ((ImageWithTag)e.Data.GetData(typeof(ImageWithTag))).Tag;//DataFormats.GetFormat("ImageWithTag")
            }
            System.Diagnostics.Debug.Write("tag1 = " + pictureBox1.Tag + "; tag2 = " + pictureBox2.Tag);
        }

Open in new window

0
 
anarki_jimbelCommented:
BTW, you may optimize my code so that no type casting done twice like below etc:

                pictureBox2.Image = ((ImageWithTag)e.Data.GetData(typeof(ImageWithTag))).Img;//DataFormats.GetFormat("ImageWithTag")
                pictureBox2.Tag = ((ImageWithTag)e.Data.GetData(typeof(ImageWithTag))).Tag;//DataFormats.GetFormat("ImageWithTag")
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could drag just the PictureBox too...I like to use a DataObject with a custom name so that I know if my app is the source of the Drag:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);

            pictureBox2.AllowDrop = true;
            pictureBox2.DragEnter += new DragEventHandler(pictureBox2_DragEnter);
            pictureBox2.DragDrop += new DragEventHandler(pictureBox2_DragDrop);
        }

        void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DataObject data = new DataObject("MyPictureBoxDrag", pictureBox1);
                pictureBox1.DoDragDrop(data, DragDropEffects.All);
            }
        }

        void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent("MyPictureBoxDrag") ? DragDropEffects.All : DragDropEffects.None;
        }

        void pictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox pb = (PictureBox)e.Data.GetData("MyData");
            pictureBox2.Image = pb.Image;
            pictureBox2.Tag = pb.Tag;
        }

    }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Oops...in the DragDrop() Event I have the wrong name.  It should be:

    PictureBox pb = (PictureBox)e.Data.GetData("MyPictureBoxDrag");
0
 
anarki_jimbelCommented:
Honestly, I'm very unhappy with the decision from the asker.  I gave completely working solution, spent quite a bit of (my own) time - and got nothing. Not even thanks. I don't need any points, and not asking for them. However, I'd advise the asker in future to be mo thankful to people who help him.

Please, this is not request for reconsidering points - I'won't like any in this case :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree anarki_jimbel...you gave a GREAT solution.

I expected the points to all go to you or possibly a split.  Anyhoo, don't let one question get you down...keep helping people!  =)
0
 
anarki_jimbelCommented:
Idle_Mind, thanks for support :)
For such cases I usually vote for splitting points.

"keep helping people! "  - I will, promise :)
0
 
panJacekAuthor Commented:
Ups!

Sorry anarki_jimbel.

I thought I gave you 250 points...

Do not know how it happened.

Thank you very much and sorry!

panJacek
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now