For the movement of a picturebox c# windows

Asked

Viewed 170 times

0

I have a picturebox and I make his movement only to the right and left, but I would like that when the end of the image reaches the edge of the form and the beginning, it is no longer possible to move it.inserir a descrição da imagem aqui

This way:

I’d like it to stay this way: inserir a descrição da imagem aqui

Follows 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);                                
        }
    }

1 answer

2

Only with these lines of code is kind of hard to deduce, but I believe you have to test the Width of the form before making the Picturebox drive

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);                                
    }
}
  • I’ll analyze and try some research.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.