1

I have some pictureboxes in the form in a List. I want them to update their position and still being able to drag the object while updating the others.

I'm doing and infinitive while loop the keep updating their positions:

while (true)
{
    Random rnd = new Random();

    pictures[0].Location = new Point(pictures[0].Location.X - rnd.Next(-2, 3), pictures[0].Location.Y + rnd.Next(0, 2));
    pictures[1].Location = new Point(pictures[1].Location.X, pictures[1].Location.Y + 1);
    this.Update();
}

The problem is that I've made this elements draggable, but the MouseDown event is not being called. Because of the infinity loop I guess, but how can I fix that ?

1
  • You have to take care when modifying UI elements from within a thread (JonasH sugested a Timer, which is also good), or you will get an exception... Commented Jan 13, 2021 at 11:59

1 Answer 1

3

You should use a timer. Set it to trigger however often you want to update the positions. This lets the main thread process other events, while updating your animation as often as needed.

How to create a timer:

   myTimer = new System.Windows.Forms.Timer();
   myTimer.Tick += TimerEventProcessor;

   // Sets the timer interval to 0.5 seconds.
   myTimer.Interval = 500;
   myTimer.Start();

and

private void TimerEventProcessor(Object myObject,
                                        EventArgs myEventArgs) {
// Your update code here
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.