0

I am trying to create an animation of shapes created by 5 or more picture boxes moving together. The shape supposed to move from one place to another. In its way there are more picture boxes. The form has a picture background. The code I used for this is the following:

//caculating image path
            double slope = (double)(474 + 30 * squareRow - originalBoard.Location.Y) / (originalBoard.Location.X - 41 + 30 * squareCol);
            int dis = originalBoard.Location.X - animArr[originalButton].Location.X;
            counter = 1;

            while (counter != dis)
            {
                for (int k = 0; k < animArr.Length; k++)
                {
                    //creating new point
                    Point xy = new Point(pointArr[k].X + counter, pointArr[k].Y - (int)Math.Ceiling(slope * (double)counter));
                    animArr[k].Location = xy;
                    //updating image
                    animArr[k].Update();
                    this.Update();
                }
                if (counter + 3 > dis)
                    counter = dis;
                else
                    counter+=3;
            }

the code has 2 issues: The shape moving extremely slow, especially when hovering over a picture box or a label. In addition, after it reaches it's destination, the form is freezing.

does anyone know why it happens and how to fix it?

  • I'm guessing you are locking up the whole UI during your animations. Animations should really be done on via a timer, which will need a start (where the calculations are made), a tick (where the movement is done), and a stop (where you tidy up). – Neil Mar 17 '20 at 10:48
  • 1
    The code is pretty fundamentally wrong, hanging the UI thread with a loop is never appropriate. It only *seems* to work due to the Update() calls. You need to use a Timer with an Interval of 15 or 31. – Hans Passant Mar 17 '20 at 11:10
  • [Already answered here.](https://stackoverflow.com/a/188377/3585500) – ourmandave Mar 17 '20 at 11:12

0 Answers0