滚动条仅显示第一个元素

时间:2015-08-19 05:02:24

标签: c# winforms scrollbar tablelayoutpanel

我有一个非常简单的窗体。从下面的图片可以看出,滚动条仅出现在第一个项目中,但是一旦我通过拖动其边框使图形变大,滚动条就会消失。这背后的原因是什么?

Scrollbar appears only for the first item

Once I make the form larger by dragging its border, the scrollbar disappears

表单的代码如下所示:

var tlp1 = new TableLayoutPanel();
tlp1.Dock = DockStyle.Top;
tlp1.AutoSize = true;
tlp1.BackColor = Color.Bisque;

var tlp2 = new TableLayoutPanel();
tlp2.Dock = DockStyle.Fill;
tlp2.BackColor = Color.DarkSeaGreen;

page.Controls.Add(tlp2);
page.Controls.Add(tlp1);

for (var i = 0; i < 10; i++)
{
    tlp1.Controls.Add(new TextBox()
    {
        Text = string.Format("page1. {0}", i)
    });

    tlp2.Controls.Add(new TextBox()
    {
        Text = string.Format("page2. {0}", i)
    });
}

page.AutoScroll = true;

1 个答案:

答案 0 :(得分:1)

可滚动区域是ScollableContainer排列后LayoutEngine添加到tlp2.Dock = DockStyle.Fill的所有控件的最大值(x +宽度,y +高度)。

ClientRectangle.Height开始,其ClientRectangle(x,y,width,height)是可用的空间。换句话说,由于米色面板占据了所有可用高度,因此绿色面板客户端高度(DockStyle.Fill)为零。当您增加主窗口的高度时,可以使用更多空间,并将额外高度分配给绿色面板。但是,当设置为tlp2.Dock = DockStyle.Bottom; tlp2.AutoSize = true; 时,绿色面板不会满足空间需求。

如果您设置以下内容:

FlowLayoutPanel

然后您将拥有一个横跨两个面板整个高度的滚动条。或者另一种方法是将面板添加到垂直class FormTab : Form { public FormTab() { TabControl tc = new TabControl { Dock = DockStyle.Fill }; TabPage page = new TabPage(); var tlp1 = new TableLayoutPanel(); tlp1.Dock = DockStyle.Top; tlp1.AutoSize = true; tlp1.BackColor = Color.Bisque; var tlp2 = new TableLayoutPanel(); tlp2.Dock = DockStyle.Bottom; tlp2.AutoSize = true; tlp2.BackColor = Color.DarkSeaGreen; VFLP p = new VFLP(); p.SuspendLayout(); p.Controls.Add(tlp2); p.Controls.Add(tlp1); page.Controls.Add(p); //page.Controls.Add(tlp2); //page.Controls.Add(tlp1); for (var i = 0; i < 10; i++) { tlp1.Controls.Add(new TextBox() { Text = string.Format("page1. {0}", i) }); tlp2.Controls.Add(new TextBox() { Text = string.Format("page2. {0}", i) }); } this.DoubleClick += delegate { Size s = page.GetPreferredSize(Size.Empty); int bp = 1; }; tc.TabPages.Add(page); page.AutoScroll = true; Controls.Add(tc); p.ResumeLayout(true); } class VFLP : FlowLayoutPanel { public VFLP() { this.BackColor = Color.AliceBlue; WrapContents = false; FlowDirection = FlowDirection.TopDown; AutoSize = true; AutoSizeMode = AutoSizeMode.GrowAndShrink; } public override Size GetPreferredSize(Size proposedSize) { Size s = base.GetPreferredSize(proposedSize); Size s2 = Size.Empty; foreach (Control c in Controls) { Size s3 = c.GetPreferredSize(Size.Empty); Padding m = c.Margin; s2.Height += s3.Height + m.Vertical; int w = s3.Width + m.Horizontal; if (w > s2.Width) s2.Width = w; } return s2; } } } ,例如

google.maps.event.addDomListener(window, 'load', initialize_map);
google.maps.event.addDomListener(window, 'resize', function() {
  var center = map.getCenter();
  google.maps.event.trigger(map, "resize");
  map.setCenter(center); 
});