I've got a Panel
that contains many smaller panels.
The containing panel has AutoScroll
set to true so that when there are too many small panels I can scroll down.
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.
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!
I've tried changing the properties to disable it but this seemingly has no effect.
I've tried always showing the vertical scrollbar (as this would be acceptable), but it only briefly showed a really ugly bar that then disappeared and was replaced by the native one.
I can see other people with the same problem but no solution.
Paste the code below into a new Winform and try the following steps:
- Press the 'Add' button 4 times to get 4 panels, they should go off the bottom of the container.
- Vertically resize the form so that there is some whitespace after the last panel.
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'.
static List<Panel> listOfPanels; static Panel panel; static bool flipflop; private void Form1_Load(object sender, EventArgs e) { Height = 400; Width = 400; listOfPanels = new List<Panel>(); 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 > 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(); }
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!
Thanks!