title: How do I paint a ball moving in a panel using WinForms?
body: <p>I have code that will paint a ball when I click the mouse in my panel. Now, what I want is when I click on the panel, not only does a ball appear, but it also moves at some velocity. Right now I don't really care if the ball travels past the panel's boundaries. How do I do this?</p>
  
  <pre><code>public partial class Form1 : Form
  {
      ArrayList dotPts = new ArrayList();
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void mainPanel_Paint(object sender, PaintEventArgs e)
      {
          foreach (Point p in dotPts)
          { 
              e.Graphics.FillEllipse(Brushes.Black, p.X, p.Y, 20, 20); 
          }
      }
  
      private void mainPanel_MouseUp(object sender, MouseEventArgs e)
      {
          Graphics g = Graphics.FromHwnd(this.Handle);
          dotPts.Add(new Point(e.X - 10, e.Y - 10));
          mainPanel.Invalidate();
      }
  }
  </code></pre>
  
  <p>InitializeComponent():</p>
  
  <pre><code>private void InitializeComponent()
      {
          this.mainPanel = new System.Windows.Forms.Panel();
          this.SuspendLayout();
          // 
          // mainPanel
          // 
          this.mainPanel.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
          this.mainPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
          this.mainPanel.Location = new System.Drawing.Point(12, 12);
          this.mainPanel.Name = "mainPanel";
          this.mainPanel.Size = new System.Drawing.Size(790, 424);
          this.mainPanel.TabIndex = 0;
          this.mainPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.mainPanel_Paint);
          this.mainPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.mainPanel_MouseUp);
          // 
          // Form1
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(821, 447);
          this.Controls.Add(this.mainPanel);
          this.Name = "Form1";
          this.Text = "Form1";
          this.ResumeLayout(false);
  
      }
  </code></pre>