2008-08-25 60 views
2

當使用.NET和WinForms創建可滾動的用戶控件時,我反覆遇到以下情況:例如,垂直滾動條彈出,重疊控件的內容,導致還需要水平滾動條。理想情況下,內容會縮小一點,爲垂直滾動條騰出空間。如何確保滾動條不會與內容重疊?

我目前的解決方案是讓我的控件保持在最右邊的40個像素左右,垂直滾動條將會佔用。由於這仍然是控件的有效客戶端空間,因此即使沒有控件完全隱藏,水平滾動條仍然會在垂直滾動條覆蓋時出現。但至少用戶實際上並不需要使用出現的水平滾動條。

有沒有更好的方法使這一切工作?一些方法來保持不需要的和不需要的滾動條顯示?

回答

1

您將需要您的控件稍微調整大小以適應垂直滾動條的寬度。通過對接實現這一目標的一種方法。而不是僅僅放在窗體上的控件,你必須玩面板,填充,最小/最大尺寸和對接。

以下是您可以放置​​在空白的新Form1後面的示例代碼。在設計器或運行時調整窗體大小,您將看到水平滾動條未顯示,且字段不重疊。我也給了一個最大寬度爲好措施的領域:

#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; 
0

如果您的控件位於面板內,請嘗試將面板的AutoScroll屬性設置爲False。這將隱藏滾動條。我希望這可以讓你指出正確的方向。

myPanel.AutoScroll = False