Skip to content
Loading
  • Rajanikant Hawaldar
    +1
    Try using windows forms timer.
    //.cs code
    1. using System;  
    2. using System.Drawing;  
    3. using System.Windows.Forms;  
    4.   
    5. namespace MovingCircleRectangle  
    6. {  
    7.     public partial class Form1 : Form  
    8.     {  
    9.         public Form1()  
    10.         {  
    11.             InitializeComponent();  
    12.         }  
    13.         //Variable declaration  
    14.         private int x1 = 4;  
    15.         Timer timer;  
    16.         private bool once = true;  
    17.         private bool up = false;  
    18.         // Form paint event to draw shapes  
    19.         private void Form1_Paint(object sender, PaintEventArgs e)  
    20.         {  
    21.             Graphics gps = e.Graphics;  
    22.             Pen redPen = new Pen(Color.Red, 3);  
    23.             Rectangle rect = new Rectangle(x1, x1, x1, x1);  
    24.             Rectangle circle = new Rectangle(x1 + 10, x1 + 10, x1 + 50, x1 + 50);  
    25.             gps.DrawRectangle(redPen, rect);  
    26.             gps.DrawEllipse(redPen, circle);  
    27.   
    28.             redPen.Dispose();  
    29.             gps.Dispose();  
    30.         }  
    31.   
    32.         private void Init()  
    33.         {  
    34.             if (timer == null)  
    35.             {  
    36.                 timer = new Timer(); // initalizing timer  
    37.                 timer.Interval = 500;  
    38.                 timer.Tick += timer_tick; // attaching tick event to timer object  
    39.             }  
    40.             timer.Start(); // starting timer  
    41.         }  
    42.   
    43.         private void btnStartProcess_Click(object sender, EventArgs e)  
    44.         {  
    45.             if (once)  
    46.             {  
    47.                 once = false;  
    48.                 Init();  
    49.             }  
    50.             else  
    51.             {  
    52.                 if (timer != null)  
    53.                 {  
    54.                     timer.Stop();  
    55.                     timer.Dispose();  
    56.                     timer = null;  
    57.                 }  
    58.                 Application.Exit(); // Shutdown app  
    59.             }  
    60.         }  
    61.         private void timer_tick(object sender, EventArgs e)  
    62.         {  
    63.             if (up)  
    64.             {  
    65.                 x1 += 10;  
    66.                 if (x1 > 100)  
    67.                     up = false;  
    68.             }  
    69.             else  
    70.             {  
    71.                 x1 -= 10;  
    72.                 if (x1 <= 10)  
    73.                     up = true;  
    74.             }  
    75.             this.Invalidate(); // Validating form  
    76.         }  
    77.     }  
    78. }  
     //Designer.cs code
    namespace MovingCircleRectangle
    {
    partial class Form1
    {
    ///
    /// Required designer variable.
    ///
    private System.ComponentModel.IContainer components = null;
    ///
    /// Clean up any resources being used.
    ///
    /// true if managed resources should be disposed; otherwise, false.
    protected override void Dispose(bool disposing)
    {
    if (disposing && (components != null))
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.btnStartProcess = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // btnStartProcess
    //
    this.btnStartProcess.Location = new System.Drawing.Point(32, 29);
    this.btnStartProcess.Name = "btnStartProcess";
    this.btnStartProcess.Size = new System.Drawing.Size(75, 23);
    this.btnStartProcess.TabIndex = 0;
    this.btnStartProcess.Text = "Start";
    this.btnStartProcess.UseVisualStyleBackColor = true;
    this.btnStartProcess.Click += new System.EventHandler(this.btnStartProcess_Click);
    //
    // Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(696, 504);
    this.Controls.Add(this.btnStartProcess);
    this.Name = "Form1";
    this.Text = "Moving Circle and Rectangle";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.Button btnStartProcess;
    }
    }
    +1
  • Rajanikant Hawaldar
    +1
    Thank you @Laxmidhar Sahoo, i was forgot to add end process button.
    +1
  • 0
    Thank you!
    The timer solved the problem.  
    0