面板垂直滚动

5

我有一个winform应用程序,在其中有一个面板控件。

enter image description here

我想要在面板内滚动并垂直地放置比当前控件高度更多的控件,然后拥有一个滚动条来帮助我查看所有的控件。我该如何实现这个功能?

以下是设计师代码,如果有人想查看代码:

private void InitializeComponent()
{
  this.panel1 = new System.Windows.Forms.Panel();
  this.SuspendLayout();
  // 
  // panel1
  // 
  this.panel1.AutoScroll = true;           
  this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
  this.panel1.Location = new System.Drawing.Point(12, 12);    
  this.panel1.Name = "panel1";
  this.panel1.Size = new System.Drawing.Size(267, 365);
  this.panel1.TabIndex = 0;
  // 
  // Form2
  // 
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  this.ClientSize = new System.Drawing.Size(456, 410);
  this.Controls.Add(this.panel1);
  this.Name = "Form2";
  this.Text = "Form2";
  this.ResumeLayout(false);
}
2个回答

6

由于您设置了AutoScroll = true,因此您不需要做任何事情。在面板中放置的任何控件都将自动在面板中创建适当的滚动距离,即使超出了可见边界。

如果您想手动覆盖它,请将AutoScroll = false并使用AutoScrollMinSize属性设置画布的大小,例如:

panel1.AutoScrollMinSize = new Size(0, 1200);

您可能需要考虑将面板固定在表单的四个边缘上,或者使用对齐方式填充整个表单,因为它看起来是一个可调整大小的表单。无论哪种方式,面板都会为您处理滚动条的大小。


0

尝试在 MDIForm 的面板中加载其他表单。它完美地运作。

myForm.TopLevel = false;
myForm.AutoScroll = true;
main_panel.Controls.Clear();
main_panel.Controls.Add(myForm);
main_panel.AutoScrollMinSize = new Size(0, myForm.Height);
myForm.Show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接