Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows forms - weird auto scroll behaviour

Tags:

c#

winforms

I got a problem in regards to autoscrolling of a system.windows.forms.panel. I have a panel that i fill with checkboxes, and if the height requirement of the total amount of checkboxes exceeds the height of the panel it should add a vertical scrollbar. My problem is that it is handles the vertical scrollbar as intended, but it also display a horizontal scrollbar wich is not needed. I adjust the width of the panel by adding System.Windows.Forms.SystemInformation.VerticalScrollBarWidth to the panel width.

int prevMainTop = 0;
int maxWidth = 0;

foreach (List<String> arr in folderArr)
{
    if (arr[0].Length * 7 > maxWidth) { maxWidth = arr[0].Length * 7; }
}

foreach (List<String> arr in folderArr)
{
    CheckBox cb = new CheckBox();
    cb.BackColor = Color.Chocolate;
    cb.Checked = true;
    cb.AutoSize = false;
    cb.Width = maxWidth;
    cb.Name = arr[0];
    cb.Text = arr[0];
    cb.Tag = arr[1];
    cb.Top = prevMainTop;
    prevMainTop = prevMainTop + 25;
    this.mainPanel.Controls.Add(cb);           
}

this.mainPanel.Width = maxWidth + System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;

Image showing the unwanted added space to the right of the checkboxes, color added to control background to illustrate the size of the control.

like image 913
Gurumannen Avatar asked Aug 16 '26 15:08

Gurumannen


1 Answers

Check the AutoScrollMargin and AutoScrollMinSize properites of the panel. AutoScrollMargin should be (0,0) and you may need to also set AutoScrollMinSize to the maxWidth value.

like image 193
Mohammad Dehghan Avatar answered Aug 18 '26 06:08

Mohammad Dehghan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!