.net 如何确保滚动条不';不重叠内容?

.net 如何确保滚动条不';不重叠内容?,.net,winforms,.net,Winforms,在使用.NET和WinForms创建可滚动用户控件时,我多次遇到这样的情况:例如,弹出一个垂直滚动条,与控件的内容重叠,导致还需要一个水平滚动条。理想情况下,内容会缩小一点,为垂直滚动条腾出空间 我目前的解决方案是将我的控件保持在最右边的40像素左右,这样垂直滚动条就会占据空间。因为这仍然是控件的有效客户端空间,所以当水平滚动条被垂直滚动条覆盖时,水平滚动条仍然会出现,即使根本没有隐藏任何控件。但至少用户实际上不需要使用弹出的水平滚动条 有没有更好的办法让这一切顺利进行?有什么方法可以防止不需要

在使用.NET和WinForms创建可滚动用户控件时,我多次遇到这样的情况:例如,弹出一个垂直滚动条,与控件的内容重叠,导致还需要一个水平滚动条。理想情况下,内容会缩小一点,为垂直滚动条腾出空间

我目前的解决方案是将我的控件保持在最右边的40像素左右,这样垂直滚动条就会占据空间。因为这仍然是控件的有效客户端空间,所以当水平滚动条被垂直滚动条覆盖时,水平滚动条仍然会出现,即使根本没有隐藏任何控件。但至少用户实际上不需要使用弹出的水平滚动条


有没有更好的办法让这一切顺利进行?有什么方法可以防止不需要或不需要的滚动条出现?

如果控件位于面板内,请尝试将面板的AutoScroll属性设置为False。这将隐藏滚动条。我希望这为你指明了正确的方向

myPanel.AutoScroll = False

您需要稍微调整控件的大小,以适应垂直滚动条的宽度。实现这一点的一种方法是通过对接。除了在表单上放置控件外,您还需要使用一些面板、填充、最小/最大大小和停靠

下面是您可以放置在空白新表单后面的示例代码1。在设计器或运行时调整窗体的大小,您将看到水平滚动条不会显示,字段也不会重叠。我还为字段指定了一个最大宽度,以便更好地测量:

#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.panel1 = new System.Windows.Forms.Panel();
        this.panel2 = new System.Windows.Forms.Panel();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.panel1.SuspendLayout();
        this.panel2.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
        this.textBox1.Location = new System.Drawing.Point(32, 0);
        this.textBox1.MaximumSize = new System.Drawing.Size(250, 0);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(250, 20);
        this.textBox1.TabIndex = 0;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Dock = System.Windows.Forms.DockStyle.Left;
        this.label1.Location = new System.Drawing.Point(0, 0);
        this.label1.Name = "label1";
        this.label1.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
        this.label1.Size = new System.Drawing.Size(32, 16);
        this.label1.TabIndex = 0;
        this.label1.Text = "Field:";
        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.textBox1);
        this.panel1.Controls.Add(this.label1);
        this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
        this.panel1.Location = new System.Drawing.Point(0, 0);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(392, 37);
        this.panel1.TabIndex = 2;
        // 
        // panel2
        // 
        this.panel2.Controls.Add(this.textBox2);
        this.panel2.Controls.Add(this.label2);
        this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
        this.panel2.Location = new System.Drawing.Point(0, 37);
        this.panel2.Name = "panel2";
        this.panel2.Size = new System.Drawing.Size(392, 37);
        this.panel2.TabIndex = 3;
        // 
        // textBox2
        // 
        this.textBox2.Dock = System.Windows.Forms.DockStyle.Top;
        this.textBox2.Location = new System.Drawing.Point(32, 0);
        this.textBox2.MaximumSize = new System.Drawing.Size(250, 0);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(250, 20);
        this.textBox2.TabIndex = 0;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Dock = System.Windows.Forms.DockStyle.Left;
        this.label2.Location = new System.Drawing.Point(0, 0);
        this.label2.Name = "label2";
        this.label2.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
        this.label2.Size = new System.Drawing.Size(32, 16);
        this.label2.TabIndex = 0;
        this.label2.Text = "Field:";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;
        this.ClientSize = new System.Drawing.Size(392, 116);
        this.Controls.Add(this.panel2);
        this.Controls.Add(this.panel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.panel1.ResumeLayout(false);
        this.panel1.PerformLayout();
        this.panel2.ResumeLayout(false);
        this.panel2.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label2;
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent(){
this.textBox1=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.panel1=new System.Windows.Forms.Panel();
this.panel2=new System.Windows.Forms.Panel();
this.textBox2=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
这个.panel1.SuspendLayout();
这个.panel2.SuspendLayout();
这个.SuspendLayout();
// 
//文本框1
// 
this.textBox1.Dock=System.Windows.Forms.DockStyle.Top;
this.textBox1.Location=新系统.Drawing.Point(32,0);
this.textBox1.MaximumSize=新系统.图纸.尺寸(250,0);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(250,20);
this.textBox1.TabIndex=0;
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Dock=System.Windows.Forms.DockStyle.Left;
this.label1.Location=新系统图点(0,0);
this.label1.Name=“label1”;
this.label1.Padding=new System.Windows.Forms.Padding(0,3,0,0);
this.label1.Size=新系统图纸尺寸(32,16);
this.label1.TabIndex=0;
this.label1.Text=“字段:”;
// 
//小组1
// 
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Dock=System.Windows.Forms.DockStyle.Top;
this.panel1.Location=新系统图纸点(0,0);
this.panel1.Name=“panel1”;
this.panel1.Size=新系统图纸尺寸(392,37);
this.panel1.TabIndex=2;
// 
//小组2
// 
this.panel2.Controls.Add(this.textBox2);
this.panel2.Controls.Add(this.label2);
this.panel2.Dock=System.Windows.Forms.DockStyle.Top;
this.panel2.Location=新系统图纸点(0,37);
this.panel2.Name=“panel2”;
this.panel2.Size=新系统图纸尺寸(392,37);
this.panel2.TabIndex=3;
// 
//文本框2
// 
this.textBox2.Dock=System.Windows.Forms.DockStyle.Top;
this.textBox2.Location=新系统.Drawing.Point(32,0);
this.textBox2.MaximumSize=新系统.图纸.尺寸(250,0);
this.textBox2.Name=“textBox2”;
this.textBox2.Size=新系统.Drawing.Size(250,20);
this.textBox2.TabIndex=0;
// 
//标签2
// 
this.label2.AutoSize=true;
this.label2.Dock=System.Windows.Forms.DockStyle.Left;
this.label2.Location=新系统图点(0,0);
this.label2.Name=“label2”;
this.label2.Padding=new System.Windows.Forms.Padding(0,3,0,0);
this.label2.Size=新系统图纸尺寸(32,16);
this.label2.TabIndex=0;
this.label2.Text=“字段:”;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll=true;
this.ClientSize=新系统.Drawing.Size(392116);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name=“Form1”;
this.Text=“Form1”;
此.panel1.ResumeLayout(错误);
此.panel1.PerformLayout();
此.panel2.ResumeLayout(错误);
此.panel2.PerformLayout();
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.label1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.label2;