title: Why does the horizontal scrollbar still appear in this panel?
body: <p>I've got a <code>Panel</code> that contains many smaller panels.<br>
  The containing panel has <code>AutoScroll</code> set to true so that when there are too many small panels I can scroll down.<br>
  The small panel's width is always set to the clientsize width of the container (allowing for borders etc) so that no horizontal scrollbar should ever be displayed.<br>
  This seems to mostly be the case unless the last sub-panel is touching the bottom of the containing panel, then the horizontal scrollbar appears when I don't want it to!</p>
  
  <p>I've tried <a href="https://stackoverflow.com/questions/5489273/how-do-i-disable-the-horizontal-scrollbar-in-a-panel">changing the properties to disable it</a> but this seemingly has no effect.<br>
  I've tried <a href="https://stackoverflow.com/questions/8690643/how-to-force-vertical-scrollbar-always-be-visible-from-autoscroll-in-winforms">always showing the vertical scrollbar</a> (as this would be acceptable), but it only briefly showed a really ugly bar that then disappeared and was replaced by the native one.<br>
  I can see <a href="https://stackoverflow.com/questions/25495183/autoscroll-on-panel-shows-incorrect-unnecessary-scrollbars">other people with the same problem</a> but no solution.</p>
  
  <p>Paste the code below into a new Winform and try the following steps:</p>
  
  <ol>
  <li>Press the 'Add' button 4 times to get 4 panels, they should go off the bottom of the container.</li>
  <li>Vertically resize the form so that there is some whitespace after the last panel.</li>
  <li><p>Now carefully vertically resize the form so that the bottom of the last panel touches the bottom of the container. It's around here that the scrollbar should appear, it might take a bit of 'wiggling'.</p>
  
  <pre><code>static List&lt;Panel&gt; listOfPanels;
  static Panel panel;
  static bool flipflop;
  
  private void Form1_Load(object sender, EventArgs e)
  {
      Height = 400;
      Width = 400;
  
      listOfPanels = new List&lt;Panel&gt;();
  
      panel = new Panel()
      {
          Height = this.ClientSize.Height - 20,
          Width = 200,
          Top = 10,
          Left = 10,
          BackColor = Color.White,
          BorderStyle = BorderStyle.FixedSingle,
          Padding = Padding.Empty,
          Margin = Padding.Empty,
          Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom,
      };
  
      // I'VE TRIED DISABLING IT HERE!
      panel.AutoScroll = false;
      panel.HorizontalScroll.Enabled = false;
      panel.HorizontalScroll.Visible = false;
      panel.AutoScroll = true;
  
      panel.Resize += panel_Resize;
  
      Button button = new Button()
      {
          Text = @"Add",
          Size = new Size(100, 50),
          Top = 10,
          Left = 20 + panel.Width
      };
      button.Click += button_Click;
  
      Controls.Add(panel);
      Controls.Add(button);
  }
  
  void panel_Resize(object sender, EventArgs e)
  {
      renderSubPanels();
  }
  
  void button_Click(object sender, EventArgs e)
  {
      Panel subPanel = new Panel()
      {
          Height = 100,
          BackColor = flipflop ? Color.PeachPuff : Color.PowderBlue,
          Top = (listOfPanels.Count * 100) - Math.Abs(panel.AutoScrollPosition.Y)
      };
  
      listOfPanels.Add(subPanel);
  
      flipflop = !flipflop;
  
      panel.Controls.Add(subPanel);
  
      renderSubPanels();
  }
  
  void renderSubPanels()
  {
      panel.SuspendLayout();
      bool verticalScrollVisible = listOfPanels.Count * 100 &gt; panel.ClientSize.Height;
      foreach (Panel p in listOfPanels)
      {
          if (verticalScrollVisible)
          {
              p.Width = panel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth - 2;
          }
          else
          {
              p.Width = panel.Width - 2;
          }
  
          p.Top = (listOfPanels.IndexOf(p) * 100) - Math.Abs(this.AutoScrollPosition.Y);
      }
      panel.ResumeLayout();
  }
  </code></pre></li>
  </ol>
  
  <p>In my real program it's actually a custom panel I'm working with so I'm open to ideas. I just want the pesky thing to go away!<br>
  Thanks!</p>