Moving a picutebox with a timer and making it loop. from new location and back. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Moving a picutebox with a timer and making it loop. from new location and back.

methaelmethael OntarioPosts: 2Member

Im trying to have a picture box move from the original location to new and back and to new and so on. im trying to use the while loop but im not sure how to do that.


  • mazanujmazanuj Ukraine, KievPosts: 18Member
    edited April 2014

    1/ create new project windows forms
    2/ add to startup folder picture "korn.jpg"
    3/ Thread.Suspend and Thread.Resume has been depricated, use Semaphore, Mutex, Monitor or Event (i don't have enough time)
    4/ if this help you >> mark as answer

    public partial class Form1 : Form
        private Button buttonStart;
        private Button buttonStop;
        private PictureBox pb;
        private bool flag;
        private System.Threading.Thread thread;
        public Form1()
            //Button start
            this.buttonStart = new Button();
            buttonStart.Location = new Point(50, 20);
            buttonStart.Text = "START";
            buttonStart.Click += new EventHandler(buttonStart_Click);
            //Button start
            this.buttonStop = new Button();
            buttonStop.Location = new Point(150, 20);
            buttonStop.Text = "STOP";
            buttonStop.Click += new EventHandler(buttonStop_Click);
            //Picture box
            this.pb = new PictureBox();
            pb.BackgroundImage = Image.FromFile("korn.jpg");
            pb.Location = new Point(10, 50);
            pb.ClientSize = new Size(200, 200);
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
            thread = new System.Threading.Thread(new System.Threading.ThreadStart(netThreadStart));
        private void buttonStart_Click(object sender, EventArgs e)
            this.flag = true;
            if (this.thread.ThreadState == System.Threading.ThreadState.Suspended)
            else this.thread.Start();
        private void buttonStop_Click(object sender, EventArgs e)
            this.flag = false;            
        private void Form_Closing(object sender, FormClosingEventArgs e)
        private void netThreadStart()
            Point pbPointStart = this.pb.Location;
            Point pbPointEnd = new Point(pbPointStart.X + 20, pbPointStart.Y);
                this.pb.Invoke((MethodInvoker)delegate { this.pb.Location = pbPointEnd; });
                this.pb.Invoke((MethodInvoker)delegate { this.pb.Location = pbPointStart; });
Sign In or Register to comment.