How do I force controls to repaint during a loop

Can anyone tell me how I make sure controls are repainted during a loop I'm trying to slide a groupBox outside the window like this (and back in again later on):

while ( groupBox1.Left < 800 )
groupBox1.Left++;

I need to make sure all contained are repainted in each loop, to make the animation look smooth.

OR - is there a better way to do this

Thanks for your help, Mikael




Answer this question

How do I force controls to repaint during a loop

  • Apolo H

    You can also put the long running process on a separate thread.

  • Olusola Abiodun

    Application.DoEvents() is what you need to force a paint.



  • OmarShahine

    And a nice long delay so you can actually see it move visually. Insert System.Threading.Thread.Sleep(30) into the loop (yuck) or use a Timer (yeah). You may need to increase the move distance to make it quick enough.



  • themcfet

    Thanks a lot for your replies - the code

    while ( groupBox1.Left < this.Width + 50 )
    {
    groupBox1
    .Left += 10;
    Application
    .DoEvents();
    }

    works like a charm. I'm now adding 10 pixels in each loop, and it works the way I want it to ...

    Thanks, Mikael



  • How do I force controls to repaint during a loop