On every click on pictureBox
, I add object in list which is type Car
.
I want to make them move as soon as i click on picutreBox
.
Here is my code
private Thread t;
private void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
{
list.Add(new Car(e.X, e.Y, 40, 40));
br++;
t = new Thread(Draw);
t.Start();
}
Draw:
private void Draw()
{
Graphics g = pictureBox1.CreateGraphics();
for(int i = 0; i < 1000; i++)
{
list[br].DrawCar(g, Color.Red);
list[br].Move();
Thread.Sleep(100);
pictureBox1.Invoke(
(MethodInvoker)delegate
{
pictureBox1.Refresh();
});
}
g.Dispose();
}
The problems i have are following:
When i click first time, it is moving nicely, but as soon as i click again , car stops and next car starts with moving.
The more i click on form, the car is moving faster, i dont know why
- When i exit from form, i get an Exception:
System.InvalidOperationException: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.'
` doesn't do anything on this site. If you want a new line, you have to use enter twice in a row. – AustinWBryan Jul 03 '18 at 03:08