dot_net

C# Application Moving An Image Using Arrow Keys of Keyboard

To run this application you have Visual Studio installed on your laptop/PC and have basic knowledge about C# language.

1. Create new project.

2. Open Windows Form Application. Name it MovingTheImage or something else you want.

1-1

3. Double Click the form1 on Solution Explorer.

2-1

4. Change the Background Color of the Form.
i. Select the form by single click on it.
ii. Right click > Properies > BackColor > Black
iii. Save (Ctrl+S).

3-1

4-1

5. Go to toolbox Double Click on timer to add timer control.

5

6. Now add Picture Box Control to the Form1.
i. Add Image to the Picture Box.
Properties>Image>Local Resource Or Project resource> Import>Ok.

6

7. Double Click on Form1. Add Following Code.
namespace MovingTheImage
{
publicpartialclassForm1 : Form
{
privateint x;
privateint y;

public Form1()
{
InitializeComponent();
x = 20;
y = 20;

}
privatevoid Form1_Load(object sender, EventArgs e)
{
timer1.Start();
pictureBox1.Location = newPoint(x, y);

}
}
}

8. Run the Application By Pressing F5 or Ctrl+F5.

7

The Image Does not Move.

9. Now Stop running the application. Double click on timer1.
Add following Code to the timer event.
privatevoid timer1_Tick(object sender, EventArgs e)
{
if (imgPosition == Positioning.Right)
{
x += 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Left)
{
x -= 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Up)
{
y -= 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Down)
{
y += 10;
pictureBox1.Location = newPoint(x, y);
}

Invalidate();

}

10. Make following changes tothe code of seventh step:-

publicpartialclassForm1 : Form
{
enumPositioning
{
Up,Down,Left,Right
}

privateint x;
privateint y;
privatePositioning imgPosition;

public Form1()
{
InitializeComponent();
x = 20;
y = 20;
imgPosition = Positioning.Right;

}

Keep rest as it is.
Run Again.
Your Image Strat Moving to the right.

8

In this Step we Will Control the movement of the Image By the arrow keys.

11. Open the properties window for Form1. Now Click on events button on top then
Find the keyDown Property Double click on it.

9

You will Reach Here.

10

12. Add following code:-

privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
imgPosition = Positioning.Left;

}
elseif (e.KeyCode == Keys.Right)
{
imgPosition = Positioning.Right;

}
elseif (e.KeyCode == Keys.Up)
{
imgPosition = Positioning.Up;
}
elseif (e.KeyCode == Keys.Down)
{
imgPosition = Positioning.Down;
}

}

Here is the final Code For it.

namespace MovingTheImage
{
publicpartialclassForm1 : Form
{
enumPositioning
{
Up,Down,Left,Right
}

privateint x;
privateint y;
privatePositioning imgPosition;

public Form1()
{
InitializeComponent();
x = 20;
y = 20;
imgPosition = Positioning.Right;

}
privatevoid Form1_Load(object sender, EventArgs e)
{
timer1.Start();
pictureBox1.Location = newPoint(x, y);

}
privatevoid timer1_Tick(object sender, EventArgs e)
{
if (imgPosition == Positioning.Right)
{
x += 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Left)
{
x -= 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Up)
{
y -= 10;
pictureBox1.Location = newPoint(x, y);
}
elseif (imgPosition == Positioning.Down)
{
y += 10;
pictureBox1.Location = newPoint(x, y);
}

Invalidate();

}

privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
imgPosition = Positioning.Left;

}
elseif (e.KeyCode == Keys.Right)
{
imgPosition = Positioning.Right;

}
elseif (e.KeyCode == Keys.Up)
{
imgPosition = Positioning.Up;
}
elseif (e.KeyCode == Keys.Down)
{
imgPosition = Positioning.Down;
}

}
}
}

11-1

Now you can run your application and see the output, if you have any query related to application you can post here.

Leave a Reply