2012-07-17 122 views
4

我寫簡單的代碼:滾動條可見

namespace Test01 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #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.panel1 = new System.Windows.Forms.Panel(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.button2 = new System.Windows.Forms.Button(); 
      this.button3 = new System.Windows.Forms.Button(); 
      this.button4 = new System.Windows.Forms.Button(); 
      this.panel1.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // panel1 
      // 
      this.panel1.AutoScroll = true; 
      this.panel1.Controls.Add(this.button4); 
      this.panel1.Controls.Add(this.button3); 
      this.panel1.Controls.Add(this.button2); 
      this.panel1.Location = new System.Drawing.Point(24, 13); 
      this.panel1.Name = "panel1"; 
      this.panel1.Size = new System.Drawing.Size(206, 43); 
      this.panel1.TabIndex = 0; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(89, 62); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 1; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // button2 
      // 
      this.button2.Location = new System.Drawing.Point(3, 3); 
      this.button2.Name = "button2"; 
      this.button2.Size = new System.Drawing.Size(75, 23); 
      this.button2.TabIndex = 2; 
      this.button2.Text = "button2"; 
      this.button2.UseVisualStyleBackColor = true; 
      // 
      // button3 
      // 
      this.button3.Location = new System.Drawing.Point(84, 3); 
      this.button3.Name = "button3"; 
      this.button3.Size = new System.Drawing.Size(75, 23); 
      this.button3.TabIndex = 3; 
      this.button3.Text = "button3"; 
      this.button3.UseVisualStyleBackColor = true; 
      // 
      // button4 
      // 
      this.button4.Location = new System.Drawing.Point(165, 3); 
      this.button4.Name = "button4"; 
      this.button4.Size = new System.Drawing.Size(75, 23); 
      this.button4.TabIndex = 4; 
      this.button4.Text = "button4"; 
      this.button4.UseVisualStyleBackColor = true; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(251, 98); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.panel1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.panel1.ResumeLayout(false); 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.Panel panel1; 
     private System.Windows.Forms.Button button4; 
     private System.Windows.Forms.Button button3; 
     private System.Windows.Forms.Button button2; 
     private System.Windows.Forms.Button button1; 
    } 
} 

和:

namespace Test01 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      panel1.Controls.Remove(button4); 
     } 
    } 
} 

我運行它,並有如下結果:

Sample

在第一列我只是按下按鈕1 - 它按預期工作。

在第二列我移動滾動條並按下button1 - button4消失,但滾動條仍然存在。當我摺疊和展開窗口滾動條消失。

在第三欄,我打開經典主題,只需按下按鈕1 - 一切OK。

我已閱讀stackoverflow1stackoverflow2stackoverflow3並嘗試使用驗證,無效,更新,刷新 - 但它沒有幫助。那麼,我該如何解決這個問題?

回答

3

刪除按鈕之前重置滾動條的位置設置爲0:

private void button1_Click(object sender, EventArgs e) 
    { 
     panel1.HorizontalScroll.Value = 0; 
     panel1.Controls.Remove(button4); 
    } 

編輯

因爲我已經在滾動條的值爲零,發現這個方法並不總是有效。我已經試過各種方法,迫使它經常清除滾動條,但似乎能可靠地工作的唯一的事情是消除按鈕之前其值設置爲1,則0:

 panel1.HorizontalScroll.Value = 1; 
     panel1.HorizontalScroll.Value = 0; 
     panel1.Controls.Remove(button4); 

這是醜陋的,但它似乎工作。我個人認爲這是一個框架中的錯誤。

+0

謝謝!這個解決方案有效 – hired777 2012-07-18 11:00:58

0

嘗試通過BeginInvoke的調用刷新方法,使得它重新進入窗口循環:

this.BeginInvoke((Action)scrollBar.PerformLayout); 

從當前方法(不調用)調用它有時避免重新呈現。

+0

它沒有工作。 – hired777 2012-07-18 11:00:07

1

嘗試用FlowLayoutPanel替代您的面板。使用與您要更換的面板相同的尺寸。在FlowLayoutPanel控件上,設置AutoScroll = TrueFlowDirection = TopDown

面板應該爲您移動控件,而且您也不必擔心滾動條。

+0

出於好奇,我只是試了一下,發現它面臨與面板完全相同的問題。看起來這種行爲是影響所有可滾動控件的「AutoScroll」屬性中的一個錯誤。 – Antagony 2012-07-18 09:54:18