I've tried to draw an object and move it, but it won't work. Where's my mistake and how can I fix it?
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int x = 100, y = 100;
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle((Brushes.Red), x, y, 20, 20);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Right)
{
x += 5;
}
if (e.KeyData == Keys.Left)
{
x -= 5;
}
if (e.KeyData == Keys.Up)
{
y -= 5;
}
if (e.KeyData == Keys.Down)
{
y += 5;
}
}
private void moveTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
}