Click here to Skip to main content
12,945,800 members (52,057 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.
Thanks,
Akkywadhwa
Posted 17-Sep-12 7:15am
Comments
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
 
good
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:
http://msdn.microsoft.com/en-us/library/system.random.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx[^].

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 vb.net[^],
Control events not firing after enable disable + multithreading[^].

Good luck,
—SA
  Permalink  
v4
Comments
Espen Harlinn 18-Sep-12 10:26am
   
5'ed!
Sergey Alexandrovich Kryukov 18-Sep-12 14:06pm
   
Thank you, Espen.
--SA
Sergey Alexandrovich Kryukov 18-Sep-12 14:07pm
   
Thank you, Meysam.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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

Best regards
Espen Harlinn
  Permalink  
Comments
Sergey Alexandrovich Kryukov 18-Sep-12 14:07pm
   
Nice and useful article referenced, my 5.
--SA
Espen Harlinn 18-Sep-12 14:11pm
   
Thank you, Sergey :-D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

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

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
OriginalGriff 4,899
CHill60 3,180
Maciej Los 2,453
Jochen Arndt 1,910
ppolymorphe 1,785


Advertise | Privacy | Mobile
Web01 | 2.8.170518.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