Home > Enterprise >  Problems with Image Tag in Picturebox
Problems with Image Tag in Picturebox

Time:09-05

So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?

i currently have 3 source images and 3 drop boxes. the dropbox6 is locked with a countdown after a buttonclick (see button2)

(later i will lock all dropboxes)

enter image description here

after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.

however, i have given each of these 3 images in the souceboxes a TAG name. how can i drop this tagname also to the dropbox?

here is what i have so far: (the Label labeled "Counter" is actually Label4 in the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            // pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox4.AllowDrop = true;
            pictureBox5.AllowDrop = true;
            pictureBox6.AllowDrop = true;
            pictureBox6.Enabled = false;


        }

        private void pictureBox4_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void pictureBox4_DragLeave(object sender, EventArgs e)
        {

        }

        private void pictureBox4_DragDrop(object sender, DragEventArgs e)
        {
            Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
            pictureBox4.Image = getPicture;
        }



        private void pictureBox5_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void pictureBox5_DragLeave(object sender, EventArgs e)
        {

        }

        private void pictureBox5_DragDrop(object sender, DragEventArgs e)
        {
            Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox5.Image = getPicture;
        }



        private void pictureBox6_DragDrop(object sender, DragEventArgs e)
        {
            Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox6.Image = getPicture;
         
            timer1.Enabled = false;
           
        }

        private void pictureBox6_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
                timer1.Enabled = false;
            }
        }

        private void pictureBox6_DragLeave(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("test");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
        
            counter--;
            if (counter == 0)
               
            timer1.Stop();
            label4.Text = counter.ToString();
            if(counter == 0)
            {
            pictureBox6.Enabled = true;
            label4.Enabled = false;
            timer1.Stop(); 
                  
            }
           
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

                
        
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show(pictureBox6.Tag.ToString());

        }

        private int counter = 3;
        private void button2_Click(object sender, EventArgs e)
        {
            int counter = 3;
            timer1 = new Timer();
            timer1.Tick  = new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // 1 second
            timer1.Start();
            label4.Text = counter.ToString();
            if(label4.Text == "0")
            {
                timer1.Stop();
            }
            pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
            button2.Visible=false;
        }
    }

    internal class bild1
    {
        private Bitmap shoreSite_d_1_l_x_x;

        public bild1(Bitmap shoreSite_d_1_l_x_x)
        {
            this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
        }
    }
}

CodePudding user response:

i'm stuck. i tried this:

 private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
           
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox4.AllowDrop = true;
            pictureBox5.AllowDrop = true;
            pictureBox6.AllowDrop = true;
            pictureBox6.Enabled = false;
            pictureBox1.Tag = "Wood";
            pictureBox2.Tag = "Stone";
            pictureBox2.Tag = "Food";

        }

and then in the drop this and getting a NULL Error

 private void pictureBox6_DragDrop(object sender, DragEventArgs e)
        {
            Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox6.Image = getPicture;
            var data = e.Data.GetData(pictureBox6.Tag.ToString());
            pictureBox6.Tag = data;
            label4.Text = data.ToString();
            timer1.Enabled = false;
        }

im starting to get anoyed.....haha im probably close and its just a tiny misstake. i have no idea...in my opinion it should work.

need help. thank you i might be able to zip the whole thing and provide a download link? is this allowed here?

CodePudding user response:

In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
    ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
    ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}

...Then make sure to parse for each possible input type in DragDrop

private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
    var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
    var tag = e.Data.GetData(DataFormats.Text) as string;
    if (image != null)
    {
        pictureBox4.Image = image;
    }
    if (tag != null)
    {
        pictureBox4.Tag = tag;
    }
    timer1.Enabled = false;
}

Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.

CodePudding user response:

AWESOME!!

It works now. I feel like i was giving Birth haha. Thank you so much Slack Groverglow. Here is the complete working Project to Play around:

Game Test

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

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


        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox4.AllowDrop = true;
            pictureBox5.AllowDrop = true;
            pictureBox6.AllowDrop = true;
            pictureBox4.Enabled = false;
            pictureBox5.Enabled = false;
            pictureBox6.Enabled = false;
            lblTag1.Text = pictureBox1.Tag.ToString();
            lblTag2.Text = pictureBox2.Tag.ToString();
            lblTag3.Text = pictureBox3.Tag.ToString();
            //pictureBox1.Tag = "Lumberjack";
            //pictureBox2.Tag = "Stone";
            //pictureBox3.Tag = "Foodfarmer";

        }

        private void pictureBox4_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;

                
            }

            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;

                timer1.Enabled = false;
            }
        }

        private void pictureBox4_DragLeave(object sender, EventArgs e)
        {

        }

        private void pictureBox4_DragDrop(object sender, DragEventArgs e)
        {
            var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
            var tag = e.Data.GetData(DataFormats.Text) as string;
            if (image != null)
            {
                pictureBox4.Image = image;
            }
            if (tag != null)
            {
                pictureBox4.Tag = tag;
            }
            lblCounter1.Text = "Building: "  tag;  // <--- variable tag is not showing

            timer1.Enabled = false;
        }



        private void pictureBox5_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;

                
            }
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;

                timer1.Enabled = false;
            }
        }

        private void pictureBox5_DragLeave(object sender, EventArgs e)
        {

        }

        private void pictureBox5_DragDrop(object sender, DragEventArgs e)
        {
            var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
            var tag = e.Data.GetData(DataFormats.Text) as string;
            if (image != null)
            {
                pictureBox5.Image = image;
            }
            if (tag != null)
            {
                pictureBox5.Tag = tag;
            }
            lblCounter2.Text = "Building: "   tag;  // <--- variable tag is not showing

            timer2.Enabled = false;
        }

        private void pictureBox6_DragEnter(object sender, DragEventArgs e)
                {
                    if (e.Data.GetDataPresent(DataFormats.Bitmap))
                    {
                        e.Effect = DragDropEffects.Copy;
                        
                    }
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;

                timer1.Enabled = false;
            }
        }

        private void pictureBox6_DragLeave(object sender, EventArgs e)
                {
                    timer3.Enabled = false;
                }

        private void pictureBox6_DragDrop(object sender, DragEventArgs e)
        {
            var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
            var tag = e.Data.GetData(DataFormats.Text) as string;
            if (image != null)
            {
                pictureBox6.Image = image;
            }
            if (tag != null)
            {
                pictureBox6.Tag = tag;
            }
            lblCounter3.Text = "Building: "   tag;  // <--- variable tag is not showing

            timer3.Enabled = false;
        }

        private int counter1 = 3;
        private void timer1_Tick(object sender, EventArgs e)
        {
            counter1--;
            if (counter1 == 0)
               
            timer1.Stop();
            lblCounter1.Text = "Finished in: "  counter1.ToString();
            if(counter1 == 0)
            {
            pictureBox4.Enabled = true;
            lblCounter1.Text = "Unlocked";
            timer1.Stop(); 
            }
        }

        private int counter2 = 3;
        private void timer2_Tick(object sender, EventArgs e)
        {

            counter2--;
            if (counter2 == 0)

                timer2.Stop();
            lblCounter2.Text = "Finished in: "   counter2.ToString();
            if (counter2 == 0)
            {
                pictureBox5.Enabled = true;
                lblCounter2.Text = "Unlocked";
                timer2.Stop();

            }

        }

        private int counter3 = 3;
        private void timer3_Tick(object sender, EventArgs e)
        {

            counter3--;
            if (counter3 == 0)

                timer3.Stop();
            lblCounter3.Text = "Finished in: "   counter3.ToString();
            if (counter3 == 0)
            {
                pictureBox6.Enabled = true;
                lblCounter3.Text = "Unlocked";
                timer3.Stop();

            }
        }

      

        private void btnConstruct1_Click(object sender, EventArgs e)
        {
            int counter1 = 3;
            timer1 = new Timer();
            timer1.Tick  = new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // 1 second
            timer1.Start();
            lblCounter1.Text = "Finished in: "  counter1.ToString();
            if (lblCounter1.Text == "0")
            {
                timer1.Stop();
            }
            pictureBox4.Image = Properties.Resources.shoreSite_d_1_l_x_x;
            btnConstruct1.Visible = false;
        }

        private void btnConstruct2_Click(object sender, EventArgs e)
        {
            int counter2 = 3;
            timer2 = new Timer();
            timer2.Tick  = new EventHandler(timer2_Tick);
            timer2.Interval = 1000; // 1 second
            timer2.Start();
            lblCounter2.Text = "Finished in: "   counter2.ToString();
            if (lblCounter2.Text == "0")
            {
                timer2.Stop();
            }
            pictureBox5.Image = Properties.Resources.shoreSite_d_1_l_x_x;

            btnConstruct2.Visible = false;
        }

        private void btnConstruct3_Click(object sender, EventArgs e)
        {
            int counter3 = 3;
            timer3 = new Timer();
            timer3.Tick  = new EventHandler(timer3_Tick);
            timer3.Interval = 1000; // 1 second
            timer3.Start();
            lblCounter3.Text = "Finished in: "   counter3.ToString();
            if (lblCounter3.Text == "0")
            {
                timer3.Stop();
            }
            pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;

            btnConstruct3.Visible = false;
        }

       

        private void btnExit_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }
    }

    internal class bild1
    {
        private Bitmap shoreSite_d_1_l_x_x;

        public bild1(Bitmap shoreSite_d_1_l_x_x)
        {
            this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
        }
    }
}

  • Related