For the movement of a picturebox c # windows

0

I have a picturebox and I only move it to the right and left, but I would like it when the end of the image reaches the edge of the form and the start, it is no longer possible to move it.

This is how it works:

I would like it to look like this:

The following is the scrpit used:

    private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button==MouseButtons.Left)
        {
            x = e.X;                
        }
    }

    private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {             
            imagemPictureBox.Left += (e.X - x);                                
        }
    }
    
asked by anonymous 02.02.2015 / 11:45

1 answer

2

Only with these lines of code it's kind of hard to figure out, but I believe you have to test the Width of the form before moving the PictureBox

private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button==MouseButtons.Left)
    {
          x = e.X;                
    }
}

private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {             
        if (x > 0 && x < this.Width)
           imagemPictureBox.Left += (e.X - x);                                
    }
}
    
10.02.2015 / 15:35