Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I like to get help on how to animate PictureBox moving to show car movement,I used timer and relocate the control box, but the animation is moving to fast.

Would any one help me to do that please..

Thanks in Advance.
Luay.
Posted
Updated 18-Jul-11 18:28pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 16:28pm    
Forms? Tag it!
--SA

Avoid using timer. Instead, use the separate thread, update the coordinates periodically with some delay using System.Threading.Thread.Sleep and notify UI thread using System.Windows.Forms.Control.Invoke or BeginInvoke. To make is smooth, use real time for calculation of coordinate in each iteration using System.DataTime.Now or System.Diagnostics.Stopwatch.

About invocation:

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Now, maybe you don't need PictureBox at all. I'm not sure it helps you. You can animate things directly using custom Control. This is how:
How do I clear a panel from old drawing[^]. Read my past answer referenced above for general idea on animation using System.Windows.Forms with System.Drawing.

—SA
 
Share this answer
 
v2
Comments
[no name] 19-Jul-11 0:29am    
Perfect Answer. My 5.Bookmarked for future Reference.
Sergey Alexandrovich Kryukov 19-Jul-11 0:37am    
Thank you, Ramalinga.
--SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900