• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

How to make a picture box move along a line.

I have a picture box  pict and two point A and B.
I want picture box move on the line connect two point A and B.
How to make this?
3 Solutions
use the location property of the picture box and set the cordinates...

now to get the coordinates on the line joining A & B, you need to use little line formula and find out the coordinates....

I can post that too, if you want...but hope you know it if you have studied geometry in school days!! (no offense here!!!)
After you have the PictureBox displayed, and want to start the motion, create and start a timer.

// Add these Class members
private Point startLocation = new Point( 0, 0); // Initialize to desired start
private Point endLocation = new Point( 10, 10); // Initialize to desired end
private int stepCount = 10;                     // Initialize to desired number of steps
private int steps = 0;                          // This is the number of times we have moved the control
This is the event handler for the timer:
private void timer_Tick(object sender, EventArgs e)
   steps += 1;
   if (steps >= stepCount) // Done
      // yourTimer.Dispose();
   double ratio = (double)steps / (double)stepCount;
   int newX = startLocation.X + (int)((double)(endLocation.X - startLocation.X) * ratio);
   int newY = startLocation.Y + (int)((double)(endLocation.Y - startLocation.Y) * ratio);
   Point current = new Point(newX, newY);
   myPictureBox.Location = current;

Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to make a design decision here...

Do you want:

(1) A fixed number of "jumps" between the points?  (this is what JimBrandley has done) Your picturebox would always take the same amount of time to travel the distance between "A" and "B" irregardless of the distance between them...whether it be 50 pixels or 1000 pixels.

(2) A fixed rate of travel between the points?  Here you would need to calculate the amount of time it would take to travel the distance and then either adjust your timer accordingly (for a fixed number of jumps), or calculate the "percentage" or the time currently transpired and determine the location that corresponds to that "time".
colorsit123Author Commented:
Thanks ashutosh_kumar for the Idea
Thanks Jim for the code
And thanks Idle_Mind for the fixed rate of travel.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now