Home > Back-end >  Move the picture top to bottom
Move the picture top to bottom

Time:12-16

How do I move a picture from top to bottom and then get it in other coordinates?

I will explain my problem more clearly: I want the image to start at the top right corner of the form and go down, then appear in the middle of the top and go down again.

I wrote the code, but it just goes down and I don't know how to proceed.

 private int x = 5;
 private void tmrMoving_Tick(object sender, EventArgs e)
 {
          
            pictureBox1.Top  = x;
            pictureBox1.Location = new Point(350,0);
                x  = 5;
            Invalidate();
}

Can anyone help me? Thanks.

CodePudding user response:

If I understand your question correctly, this may be what you're looking for. Basically you need to start on top, then slowly move to bottom. When you touch the bottom, go back to the middle of the container and go down once more. This code does not place the picture on the right but you should be able to do it yourself if you understand what the code does ;)

public partial class Form1 : Form
{
    private readonly System.Threading.Timer timer;
    private const int dtY = 5;
    private bool resetOnce;

    public Form1()
    {
        InitializeComponent();
        timer = new System.Threading.Timer(Callback, null, 0, 50);
    }

    private void Callback(object state)
    {
        BeginInvoke((MethodInvoker)delegate 
        {
            pictureBox1.Location = new Point(0, pictureBox1.Location.Y   dtY);

            // when it touches the bottom of the container
            if (pictureBox1.Location.Y   pictureBox1.Size.Height > pictureBox1.Parent.Height) 
            {
                // we already reset once, so no more going back up: stop the timer
                if (resetOnce)
                {
                    timer.Change(Timeout.Infinite, Timeout.Infinite);
                }
                // we did not reset yet, so go to middle of container
                else
                {
                    resetOnce = true;
                    pictureBox1.Location = new Point(0, pictureBox1.Parent.Height / 2 - pictureBox1.Size.Height / 2);
                }
            }
        });
    }
}
  • Related