Click here to Skip to main content
15,672,517 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
Hi everyone,
Can anybody tell me that how can i make a picturebox to move in random directions very precisely and smoothly(i guess using threads or timer) in c#(visual studio 2008). It would be appreciable if anybody can submit code with explanation rather than a project made by any expert.
[no name] 17-Sep-12 13:21pm    
What have you tried? Define "precisely". What would you consider an "expert"? Why does anyone have to submit code?

Certainly, you need to use the thread. For a simple example, please see my past answer:
Changes step by step to show a control[^].

Only instead of label, use your PictureBox object. For a random move, use the class System.Random:[^].

But don't just jump to random coordinate. Instead, jump to a small random vector relative to a current location of you moving object, just a few pixels. Generate a random value for Δy and Δx in the range, say, -5.. +5 pixels and move by this value:
myPictureBox.Left += deltaX;
myPictureBox.Top += deltaY;

To fight quite possible flicker, use double buffering:[^].

With the UI, the key here is using the UI invocation mechanism or System.Threading.Dispatcher. For more information, please see 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[^],
Control events not firing after enable disable + multithreading[^].

Good luck,
Share this answer
Espen Harlinn 18-Sep-12 10:26am    
Sergey Alexandrovich Kryukov 18-Sep-12 14:06pm    
Thank you, Espen.
[no name] 18-Sep-12 13:39pm    
Yep, I was thinking of this.
You have my 5!
Sergey Alexandrovich Kryukov 18-Sep-12 14:07pm    
Thank you, Meysam.
You could take a look at the Windows Animation API[^]

Best regards
Espen Harlinn
Share this answer
Sergey Alexandrovich Kryukov 18-Sep-12 14:07pm    
Nice and useful article referenced, my 5.
Espen Harlinn 18-Sep-12 14:11pm    
Thank you, Sergey :-D

Try this:
Random rnd1 = new Random();
private void timer1_Tick(object sender, EventArgs e)
    timer1.Enabled = false;

    int x = rnd1.Next(0, Width - pictureBox1.Width);
    int y = rnd1.Next(0, (Height - 28) - pictureBox1.Height);

    pictureBox1.Location = new Point(x, y);

    timer1.Enabled = true;
Share this answer
[no name] 17-Sep-12 16:45pm    
I think he said random direction (smoothly & precisely) not random location.
Sergey Alexandrovich Kryukov 17-Sep-12 21:02pm    
Right. Please see my comment below. Hope you will find my answer more appropriate; please see.
Sergey Alexandrovich Kryukov 17-Sep-12 20:45pm    
If also does not cover animation. You did not say what do you use to change it. Who knows what is timer1_Tick? This is not the event; only the name suggests it's some event handler, but the code where you add the handler is not shown. I know at least 4 event types -- so what is that? Besides, timer events are really bad. Multithreading is way more straightforward.
NolansPapa 11-Oct-18 15:26pm    
This is of interest to me as well. I am just learning, but my "mentor" has tasked me with creating a dice game. I always over think things in terms of what I want my program to do, so I am thinking of animating the dice as they roll. Thanks for the info!

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