Public partial class Imagemove:form
{
Public imagemove ()
{
InitializeComponent ();
}
int x = 1;//x to 1 for horizontal to right, 1 for horizontal to left
int y = 1;//y is 1 for vertical downward movement, 1 for vertical upward movement
int move = 5;//The distance between each move, horizontal and vertical are the same
private void Timer1_Tick (object sender, EventArgs e)
{
//Set the position of the picture according to the X value
if (x = = 1)
{
This.pictureBox1.Left + = move;
}
Else
{
This.pictureBox1.Left-= move;
}
//Set picture position according to Y value
if (y = = 1)
{
This.pictureBox1.Top + = move;
}
Else
{
This.pictureBox1.Top-= move;
}
//Horizontal Direction
//Hit the right (minus 20 mainly considering that the form has a border)
if (x==1&&this.picturebox1.left+this.picturebox1.width>=this. WIDTH-20)
{
x =-1;
}
//Hit the left
else if (x = =-1 && this.pictureBox1.Left <= 0)
{
x = 1;
}
//Vertical Direction
//Hit the end (37 represents the height of the title bar)
if (y==1&&this.picturebox1.top+this.picturebox1.height>= this. HEIGHT-37)
{
y =-1;
}
//Hit the top
else if (y==-1&&this.picturebox1.top <= 0)
{
y = 1;
}
}
}