Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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?

Thanks

I want picture box move on the line connect two point A and B.

How to make this?

Thanks

3 Solutions

// 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();

return;

}

double ratio = (double)steps / (double)stepCount;

int newX = startLocation.X + (int)((double)(endLocation

int newY = startLocation.Y + (int)((double)(endLocation

Point current = new Point(newX, newY);

myPictureBox.Location = current;

}

Jim

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".

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

Join Now
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!!!)