0

I try to do a program in c# where u try to decorate a christmas tree and i want to pick the globe with mouse and put on the christmas tree. I can do this but just for one picturebox after i write the code for 2nd picturebox and i run the program the globe is moving in slomotion. This is my code. How can fix this ?
P.S. picturebox1 and 2 are the globe and pb_brad is the christmas tree.

   public partial class Form1 : Form
{
    Point location = Point.Empty;
    public Form1()
    {
        InitializeComponent();
        pictureBox1.Parent = pb_brad;
        pictureBox1.BackColor = Color.Transparent;
        pictureBox2.Parent = pb_brad;
        pictureBox2.BackColor = Color.Transparent;

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            location = new Point(e.X, e.Y);
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (location != Point.Empty)
        {
            Point newlocation = this.pictureBox1.Location;
            newlocation.X += e.X - location.X;
            newlocation.Y += e.Y - location.Y;
            this.pictureBox1.Location = newlocation;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        location = Point.Empty;
    }

    private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            location = new Point(e.X, e.Y);
        }
    }

    private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
    {
        if (location != Point.Empty)
        {
            Point newlocation = this.pictureBox1.Location;
            newlocation.X += e.X - location.X;
            newlocation.Y += e.Y - location.Y;
            this.pictureBox2.Location = newlocation;
        }
    }

    private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
    {
        location = Point.Empty;
    }

}
Siespi
  • 51
  • 6

1 Answers1

0

I see that you are using pictureBox1 in the MouseMove event handler of pictureBox2.

BTW, I see that handlers for both PictureBoxes are the same. Why don't use the same handlers for all PictureBoxes and get the current PictureBox via the sender parameter?

Gosha_Fighten
  • 3,838
  • 1
  • 20
  • 31
  • I am a beginer i dont know how to do this ... Can you please put the code ? – Siespi Nov 22 '14 at 20:59
  • @Joey as a beginner, you should always try to do it yourself first, if you can't figure out in like 10-20 min then ask again – Steve Nov 22 '14 at 21:00
  • I've found these articles: http://stackoverflow.com/questions/14479143/what-use-object-sender-eventargs-e-parameters-give and http://stackoverflow.com/questions/1530867/understanding-c-sharp-events-use-of-sender-object that provide some examples. BTW, changing pictureBox1 to pictureBox2 in pictureBox2_MouseMove resolved the issue? – Gosha_Fighten Nov 22 '14 at 21:02