Howdy, Stranger!

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

Categories

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.

Comments

  • mazanujmazanuj Ukraine, KievPosts: 18Member
    edited April 14

    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()
        {
            InitializeComponent();
            //Button start
            this.buttonStart = new Button();
            buttonStart.Location = new Point(50, 20);
            buttonStart.Text = "START";
            buttonStart.Click += new EventHandler(buttonStart_Click);
            this.Controls.Add(buttonStart);
            //Button start
            this.buttonStop = new Button();
            buttonStop.Location = new Point(150, 20);
            buttonStop.Text = "STOP";
            buttonStop.Click += new EventHandler(buttonStop_Click);
            this.Controls.Add(buttonStop);
            //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.Controls.Add(pb);
            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)
                this.thread.Resume();
            else this.thread.Start();
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.flag = false;            
            this.thread.Suspend();
        }
        private void Form_Closing(object sender, FormClosingEventArgs e)
        {            
            this.thread.Abort();
        }
        private void netThreadStart()
        {
            Point pbPointStart = this.pb.Location;
            Point pbPointEnd = new Point(pbPointStart.X + 20, pbPointStart.Y);
            while(this.flag)
            {
                this.pb.Invoke((MethodInvoker)delegate { this.pb.Location = pbPointEnd; });
                System.Threading.Thread.Sleep(500);
                this.pb.Invoke((MethodInvoker)delegate { this.pb.Location = pbPointStart; });
                System.Threading.Thread.Sleep(500);
            }
        }        
    }
Sign In or Register to comment.