Gurumannen
Gurumannen

Reputation: 33

Windows forms - weird auto scroll behaviour

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.

Upvotes: 3

Views: 3428

Answers (1)

Moha Dehghan
Moha Dehghan

Reputation: 18472

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.

Upvotes: 2

Related Questions