Click here to Skip to main content
Rate this: bad
good
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 8:15am
Comments
Wes Aday at 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 at 18-Sep-12 10:26am
   
5'ed!
Sergey Alexandrovich Kryukov at 18-Sep-12 14:06pm
   
Thank you, Espen.
--SA
Sergey Alexandrovich Kryukov at 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 at 18-Sep-12 14:07pm
   
Nice and useful article referenced, my 5.
--SA
Espen Harlinn at 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 at 17-Sep-12 21:02pm
   
Right. Please see my comment below. Hope you will find my answer more appropriate; please see.
--SA
Sergey Alexandrovich Kryukov at 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

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

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 /\jmot 189
4 Praneet Nadkar 173
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2014
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