0

I have a pictureBox in another pictureBox. I try to create an imitation of flying plane on a map. I made a simple loop to do this task. The code:

for (var i = 0; i < 23; i++)
        {
                Fuel -= 1;
                Changed(i);               
        }

private void Changed(int a)
    {
        Thread.Sleep(350);
        pbPlane1.Location = new Point(525-(25*a), 235);
        pbPlane1.Refresh();
    }

So in this case the plane image is moving as I want but the old images are shown aswell, and I don't want it. I have tried a couple of things but with no results. What is wrong with my code?

Agasi Mkhitaryan
  • 162
  • 2
  • 11
  • It's not clear from your code how you are updating the images. You can use something like this to clear the picturebox. `Graphics graphic = Graphics.fromimage(pictbox.Image); graphic.Clear(Color.Red)//Color to fill the background and reset the box` http://stackoverflow.com/a/5856442/1274820 It's probably a better idea to draw the plane images using the `Graphics.fromImage` method on a bitmap and then use the picturebox to display the bitmap. I would do something like that instead of having a picturebox inside of a picturebox. – user1274820 Jan 20 '16 at 17:53
  • What if you clear your picturebox before adding the new one? – Just Do It Jan 20 '16 at 17:58
  • @JustDoIt I don't really add a new image, I want to simply change its location. If I do `pbPlane1.Image=null` then I won't see my plane – Agasi Mkhitaryan Jan 20 '16 at 18:03
  • I've found a simple answer, thank you guys for helping – Agasi Mkhitaryan Jan 20 '16 at 18:09
  • Oh okay, I misunderstood your question – Just Do It Jan 20 '16 at 18:10

1 Answers1

0

Instead of refreshing the plane image, you need to refresh the map image. The code should be as follows:

private void Changed(int a)
{
    Thread.Sleep(350);
    pbPlane1.Location = new Point(525-(25*a), 235);
    pbMap1.Refresh();      // refresh the background picturebox
}
Agasi Mkhitaryan
  • 162
  • 2
  • 11