【发布时间】:2015-10-16 01:32:44
【问题描述】:
我有一个 Panel,其中包含许多较小的面板。
包含面板将 AutoScroll 设置为 true,这样当小面板过多时,我可以向下滚动。
小面板的宽度始终设置为容器的客户端宽度(允许边框等),因此不应该显示水平滚动条。
除非最后一个子面板接触到包含面板的底部,否则这似乎大多是这种情况,然后水平滚动条会在我不想要的时候出现!
我试过changing the properties to disable it,但这似乎没有效果。
我试过always showing the vertical scrollbar(因为这是可以接受的),但它只是短暂地显示了一个非常丑陋的条,然后消失并被原生条取代。
我可以看到other people with the same problem,但没有解决方案。
将以下代码粘贴到新的 Winform 中,然后尝试以下步骤:
- 按“添加”按钮 4 次以获得 4 个面板,它们应该离开容器的底部。
- 垂直调整表单大小,以便在最后一个面板之后有一些空白。
-
现在小心地垂直调整表单的大小,使最后一个面板的底部接触到容器的底部。滚动条应该出现在这里,它可能需要一些“摆动”。
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(); }
在我的实际程序中,它实际上是一个我正在使用的自定义面板,因此我对想法持开放态度。我只是想让讨厌的事情消失!
谢谢!
【问题讨论】:
-
我仍然不明白你想要做什么。因此,如果您尝试调整主窗体的大小并且父面板的长度足以容纳所有子面板,那么父面板上的垂直滚动将被禁用,对吗?