Click here to Skip to main content
12,746,406 members (30,664 online)
Rate this:
Please Sign up or sign in to vote.
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.
Posted 17-Sep-12 8:15am
Wes Aday 17-Sep-12 13:21pm
What have you tried? Define "precisely". What would you consider an "expert"? Why does anyone have to submit code?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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,
Espen Harlinn 18-Sep-12 10:26am
Sergey Alexandrovich Kryukov 18-Sep-12 14:06pm
Thank you, Espen.
Sergey Alexandrovich Kryukov 18-Sep-12 14:07pm
Thank you, Meysam.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You could take a look at the Windows Animation API[^]

Best regards
Espen Harlinn
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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;
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.
Sergey Alexandrovich Kryukov 17-Sep-12 21:02pm
Right. Please see my comment below. Hope you will find my answer more appropriate; please see.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100