title: scrolling a panel with custom drawn controls
body: <p>I have a panel that has a collection of custom drawn controls. I know how to programmically scroll the panel but the problem is how the controls draw when scrolling. when scrolling right the control shows as normal but when scrolling left it is not looking correctly. This just the start of the complete application so I just have some basic testing in place right now. Need to find a way for the control to draw correctly when scrolling.</p>
  
  <p>Form code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      Panel pn;
      private int location = 0;
      public Form1()
      {
          InitializeComponent();
          pn = new Panel()
          {
              Width = this.ClientRectangle.Width - 20,
              Height = 120,
              BackColor = Color.Black,
              Left = 5,
              Top = 20
          };
          pn.AutoScroll = false;
          pn.VerticalScroll.Maximum = 100;
          pn.HorizontalScroll.Maximum = this.ClientRectangle.Width - 100;
          pn.VerticalScroll.Visible = false;
          pn.HorizontalScroll.Visible = false;
          pn.AutoScrollPosition = new Point(0, 0);
          pn.AutoScroll = true;
          this.KeyPreview = true;
          this.KeyDown += new KeyEventHandler(keyPress);
          for(int i = 0; i&lt;10;i++)
          {
              CustomControl1 cc = null;
              if (i % 2 != 0)
                  cc = new CustomControl1()
                  {
                      isOdd = true,
                      Width = 100,
                      Height = 100,
                      Left = (100*i)+5,
                      Top = 0
                  };
              else
                  cc = new CustomControl1()
                  {
                      isOdd = false,
                      Width = 100,
                      Height = 100,
                      Left = (100 * i) + 5,
                      Top = 0
                  };
              pn.Controls.Add(cc);
          }
          this.Controls.Add(pn);
      }
      private void keyPress(object sender, KeyEventArgs e)
      {
          switch (e.KeyCode)
          {
              case Keys.A:
                  if(location - 20 &gt;0)
                  {
                      location -= 20;
                      pn.HorizontalScroll.Value = location;
                  }
                  else
                  {
                      location = 0;
                      pn.AutoScrollPosition = new Point(location, 0);
                  }
                  break;
              case Keys.D:
                  if(location +20 &lt; pn.HorizontalScroll.Maximum)
                  {
                      location += 20;
                      pn.HorizontalScroll.Value = location;
                  }
                  else
                  {
                      location = pn.VerticalScroll.Maximum;
                      pn.AutoScrollPosition = new Point(location, 0);
                  }
                  break;
          }
          foreach(Control c in pn.Controls)
          {
              c.Invalidate();
          }
      }
  }
  </code></pre>
  
  <p>Control Code:</p>
  
  <pre><code>public partial class CustomControl1 : Control
  {
      public bool isOdd { get; set; }
      public CustomControl1()
      {
          InitializeComponent();
      }
  
      protected override void OnPaint(PaintEventArgs pe)
      {
          Graphics gr = pe.Graphics;
          Rectangle rc = new Rectangle(pe.ClipRectangle.X,pe.ClipRectangle.Y,100,100);
          Brush br = new SolidBrush(Color.Red);
          if (isOdd)
              br = new SolidBrush(Color.Yellow);
          gr.FillEllipse(br, rc);
          base.OnPaint(pe);
      }
      protected override void OnPaintBackground(PaintEventArgs pe)
      {
          base.OnPaintBackground(pe);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6QBub.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6QBub.png" alt="enter image description here"></a></p>