title: C# winforms problems drawing to picturebox using collection?
body: <p>The form contains a picturebox (picturebox1) and a timer control (timer1)...</p>
  
  <p>Basically at start up I create a List collection containing 5 instances of my pixel class. When the timer triggers I call a refresh of the picturebox which calls the paint event. In the paint event I iterate through the list collection and call each pixel's draw method.</p>
  
  <p>The problem I am having is that only one pixel shows up...that is.. unless I set a break at the point where I am adding the pixel, continue, and break again and repeat until all pthe pixels are created. Then for some reason all of the pixels show up...</p>
  
  <p>Can someone tell me why I can only see a single pixel?</p>
  
  <pre><code>public partial class Form1 : Form
  {
      List&lt;Pixel&gt; pixels = new List&lt;Pixel&gt;();
  
      public Form1()
      {            
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          for (int ii = 0; ii &lt; 5; ii++)
              pixels.Add(new Pixel(pictureBox1));  // &lt;- breakpoint here...?
      }
  
      private void pictureBox1_Paint(object sender, PaintEventArgs e)
      {
          foreach (Pixel p in pixels)
              p.Draw(e, pictureBox1);
      }
  
      private void timer1_Tick(object sender, EventArgs e)
      {
          pictureBox1.Refresh();
      }
  }
  
  public class Pixel
  {
      Random r = new Random(DateTime.Now.Millisecond);
  
      public Point Position { get; set; }
  
      public Pixel(PictureBox src) 
      {
          Position = new Point(r.Next(0, src.Width), r.Next(0, src.Height));
      }
  
      public void Draw(PaintEventArgs e, PictureBox src)
      {
          e.Graphics.DrawRectangle(new Pen(Color.Black), Position.X, Position.Y, 1, 1);
      }
  }
  </code></pre>
  
  <p>My original code does a lot more.. but I stripped it all out and got the same results.</p>