How to make a picture box move along a line.

Posted on 2007-09-29
Last Modified: 2013-12-17
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?
Question by:colorsit123
    LVL 6

    Accepted Solution

    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!!!)
    LVL 22

    Assisted Solution

    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;

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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".

    Author Comment

    Thanks ashutosh_kumar for the Idea
    Thanks Jim for the code
    And thanks Idle_Mind for the fixed rate of travel.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now