2

I'm very new to C# but trying to learn so bear with me if my syntax isn't accurate. I am able to create a picturebox with a button and it appears on screen. I can then move it around the screen just fine with a mouse down / mouse move function. I then hit the button to instantiate another picturebox to be created and can move that one around as well, but when I try to move the first picturebox the second one moves instead and goes insane. Is there a way to reference or tag the boxes on instantiation so that when I click on any of them I can move them around the screen?

public partial class Form1 : Form
{
    Point MP;
    private static Control PB;        

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int picSizeX = Properties.Resources.police.Width / 3;
        int picSizeY = Properties.Resources.police.Height / 3;

        PictureBox pb = new PictureBox();
        pb.Location = new Point(100, 100);
        pb.Size = new Size(picSizeX, picSizeY);
        pb.Image = new Bitmap(Properties.Resources.police);
        pb.SizeMode = PictureBoxSizeMode.StretchImage;
        Controls.Add(pb);
        pb.Tag = "veh";
        PB = pb;

        pb.MouseDown += Pb_MouseDown;
        pb.MouseMove += Pb_MouseMove;
        pb.MouseHover += Pb_MouseHover;
    }

    private void Pb_MouseHover(object sender, EventArgs e)
    {
        PB.MouseHover += PB_MouseHover;
    }

    private void PB_MouseHover(object sender, EventArgs e)
    {

    }

    private void Pb_MouseDown(object sender, MouseEventArgs e)
    {
        MP = e.Location;
    }

    private void Pb_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
           PB.Left = e.X + PB.Left - MP.X;
           PB.Top = e.Y + PB.Top - MP.Y;
        }
    }
}
Hassan
  • 5,360
  • 2
  • 22
  • 35
Rob
  • 25
  • 7

1 Answers1

3

Actually there is no need to have Control at class level.

In the event method there is a parameter called object sender that contains a reference to the control/object that raised the event.

Point MP;
//private Control PB; //commented out as it is not required

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
     int picSizeX = Properties.Resources.police.Width / 3;
     int picSizeY = Properties.Resources.police.Height / 3;

     PictureBox pb = new PictureBox();
     pb.Location = new Point(100, 100);
     pb.Size = new Size(picSizeX, picSizeY);
     pb.Image = new Bitmap(Properties.Resources.police);
     pb.SizeMode = PictureBoxSizeMode.StretchImage;
     Controls.Add(pb);
     pb.Tag = "veh";
     //PB = pb;

     pb.MouseDown += Pb_MouseDown;
     pb.MouseMove += Pb_MouseMove;
     pb.MouseHover += Pb_MouseHover;
}

private void Pb_MouseHover(object sender, EventArgs e)
{
     Control pbObj = sender as PictureBox; //sender refers to control that raised the event
     pbObj.MouseHover += PB_MouseHover;
}

private void PB_MouseHover(object sender, EventArgs e)
{

}

private void Pb_MouseDown(object sender, MouseEventArgs e)
{
    MP = e.Location;
}

private void Pb_MouseMove(object sender, MouseEventArgs e)
{
     Control pbObj = sender as PictureBox; //sender refers to control that raised the event

     if (e.Button == MouseButtons.Left)
     {
         pbObj.Left = e.X + pbObj.Left - MP.X;
         pbObj.Top = e.Y + pbObj.Top - MP.Y;
     }
}
Hassan
  • 5,360
  • 2
  • 22
  • 35
  • 1
    I could kiss you. This is exactly the answer I have been looking for for so long. I have some more learning to do to understand it, but it works and I am happy. Thank you. – Rob Jul 19 '16 at 18:04