Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form.

I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image.

How I can show image smoothly transition to the final location?

Here is my code so far:

public partial class Form1 : Form
{
    private int counter = 0;

    void timer_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter == 1)
        {
            pictureBox1.Show();
            timer1.Stop();
            counter = 0;
        }
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        while(i<=100){

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

             pictureBox1.Location = new Point(x+25, y);
             timer1.Start();
        }
     }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Does this work? Sorry, I can't test it where I am right now (on netbook without VS).

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+25, y);

        if (x > this.Width)
            timer1.Stop();
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Show();
        timer1.Start();
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...