2013-02-25 61 views
2

我有一个关于system.windows.forms.panel自动滚动的问题。我有一个面板,我用复选框填充,如果复选框总数的高度要求超过面板的高度,它应该添加一个垂直滚动条。我的问题是,它是按预期处理垂直滚动条,但它也显示一个不需要的水平滚动条。我通过将System.Windows.Forms.SystemInformation.VerticalScrollBarWidth添加到面板宽度来调整面板的宽度。Windows窗体 - 奇怪的自动滚动行为

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.

回答

1

检查面板的AutoScrollMarginAutoScrollMinSize性质在。 AutoScrollMargin应该是(0,0),您可能还需要将AutoScrollMinSize设置为maxWidth的值。

+0

由于某种原因,保证金设置为(50,0)...噢,有时你只是对明显的盲目失明。谢谢! – Gurumannen 2013-02-25 12:00:57