2

I’ve written an application to move an image on the form with PictureBox. But my code only move it horizontally…I used one Timer.

I need to move the image horizontally from initial point (e.g. X0,Y0) to reach an exact location (e.g. (Xc,Y0) ) then move it vertically up or down to reach (Xc,Ym) and then move back it horizontally to reach (Xf,Ym).

I have written that part that move image horizontally to reach (Xc,Y0) but I don’t know how to write the others…

Here is my code that move from (X0,Y0) to (Xc,Y0):

public partial class Form1 : Form
{
    void timer_Tick(object sender, EventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        pictureBox1.Location = new Point(x + 2, y);

        if (x > 500)
            timer1.Stop();
    }

    public Form1()
    {
        InitializeComponent();

        pictureBox1.ImageLocation = "1.png";
        pictureBox1.Size = new Size(36, 35);

        timer1.Interval = 15;

        timer1.Start();
        timer1.Tick += new EventHandler(timer_Tick);

    }
}

Besides, I have did some attempts but didn’t get any result…

Here is my attempts (try to change method timer_Tick ) :

    void timer_Tick2(object sender, EventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        if (x <= 500)
            pictureBox1.Location = new Point(x + 2, y);

        if (x > 500)
        {
            if (y <= 250)
            pictureBox1.Location = new Point(x, y + 1);

            if (y == 250)
            {
                pictureBox1.Location = new Point(x - 2, y);
                if (x < 50)
                    timer1.Stop();
            }

        }
    }

Please guys help me to complete this...

2
  • I would suggest making a new variable t for time, which would be incremented every tick. You would then compute x and y from that and it would probably be clearer.
    – Justin
    Feb 1, 2011 at 15:04
  • @ Justin - Can you please explain more?!
    – ha.M.ed
    Feb 1, 2011 at 16:44

1 Answer 1

1

The key is to use time rather than x as your key:

int t = 0;

void timer_Tick1(object sender, EventArgs e)
{
    t++;

    int x = pictureBox1.Location.X;
    int y = pictureBox1.Location.Y;

    if (t <= 250)//go right 500 in 250 ticks
        pictureBox1.Location = new Point(x + 2, y);
    else if (t <= 500)//...then go down 250 in 250 ticks
        pictureBox1.Location = new Point(x, y + 1);
    else if (t <= 750)//...then go left 500 in 250 ticks
        pictureBox1.Location = new Point(x - 2, y);
    else
        timer1.Stop();
}

because when you attempt to decrement x on the way back to x0, you will fall back into your first if block.

5
  • @ Eric Mickelsen- Thanks for your help...I guessed that I fall into first "if block". But I don't understand your advice completely, can you please complete your code?!
    – ha.M.ed
    Feb 1, 2011 at 16:17
  • @thehero: I've made an edit with more code. I decided to go with defining the new point in terms of the previous one, in order to be more in line with your thinking. It is also possible to define the point purely in terms of t (which is probably a better approach). Since your motion never crosses the same point twice, it would also be possible to fix your code without introducing t - but it's better this way. Feb 1, 2011 at 17:06
  • @ Eric Mickelsen- Thanks, I've tested your code right now but the image just move to right and stop there...Where's the problem?!!!
    – ha.M.ed
    Feb 1, 2011 at 17:36
  • @thehero: Are you sure you're using the right handler? You can use a breakpoint to ensure that the correct code is being called. Or, you can post a complete listing of your code. Feb 1, 2011 at 17:47
  • @ Eric Mickelsen- Sorry...I found my mistake!!! Your code is correct! Thanks a lot for your help!
    – ha.M.ed
    Feb 1, 2011 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.