如何定义windows forms中的static控件类型?
因为特殊需要,我用手工修改了,即把:
1.private System.Windows.Forms.StatusBar statusBar1;
2.this.statusBar1 = new System.Windows.Forms.StatusBar();
修改成:
1.private static System.Windows.Forms.StatusBar statusBar1;
2.statusBar1 = new System.Windows.Forms.StatusBar();但当窗体上增加或删除其它控件时,
所有的static控件全部都还原了,又变成了:
private System.Windows.Forms.StatusBar statusBar1;
this.statusBar1 = new System.Windows.Forms.StatusBar();

解决方案 »

  1.   

    虽然不太一样,不过对你应该有帮助:    class Frm_WMP_Factory
        {
            static Frm_WMP Frm_Player;
            public static Frm_WMP GetFrm_WMP()
            {
                if (Frm_Player == null)
                {
                    Frm_Player = new Frm_WMP();
                    Frm_Player.Disposed += new EventHandler(Frm_Player_Disposed);
                }
                return Frm_Player;
            }
            static void Frm_Player_Disposed(object sender, EventArgs e)
            {
                Frm_Player = null;
            }
        }
    //Frm_WMP是一个继承了From的类,我用来放播放器的;//静态工厂的设计模式
      

  2.   

    就是一个指针指向一个实例,该实例如果被Dispose的话把该指针置为null
      

  3.   

    我也碰到使用 static 類型控件的問題
    我把問題描述一下
    (1)將窗體Form1上托放一個 listBox1
     [會自動生成]
                namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 設計工具所需的變數。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清除任何使用中的資源。
            /// </summary>
            /// <param name="disposing">如果應該公開 Managed 資源則為 true,否則為 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form 設計工具產生的程式碼        /// <summary>
            /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
            ///
            /// </summary>
            private void InitializeComponent()
            {
                this.listBox1 = new System.Windows.Forms.ListBox();
                this.SuspendLayout();
                // 
                // listBox1
                // 
                this.listBox1.FormattingEnabled = true;
                this.listBox1.Location = new System.Drawing.Point(84, 61);
                this.listBox1.Name = "listBox1";
                this.listBox1.Size = new System.Drawing.Size(120, 95);
                this.listBox1.TabIndex = 0;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.listBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.ListBox listBox1;
        }
    }(2) 將上面自動生成的代碼修改一下  需要修改的地方有
               listBox1 = new System.Windows.Forms.ListBox();
                // 
                // listBox1
                // 
                listBox1.FormattingEnabled = true;
                listBox1.Location = new System.Drawing.Point(84, 61);
                listBox1.Name = "listBox1";
                listBox1.Size = new System.Drawing.Size(120, 95);
                listBox1.TabIndex = 0;
                this.Controls.Add(listBox1);
    //實際上把listBox1控件前面的this.去掉了 
    public static  System.Windows.Forms.ListBox listBox1;
    (3)新增一個類Class1.cs
    namespace WindowsApplication1
    {
       public  class Class1
        {
           public Class1()
           {
               //從我新增的類中去訪問static 類型的控件
               WindowsApplication1.Form1.listBox1.Items.Add("我可以添加到Form1窗體中的static類型控件listBox1中去");
           }
        }
    }(4)雙擊Form1 在load事件裡面添加一行代碼
    Class1 test = new Class1();
    問題是 listBox1前面自動又加上了this. 並且
    public static  System.Windows.Forms.ListBox listBox1;
    變成了public   System.Windows.Forms.ListBox listBox1;
    導致無法編譯
    去掉listBox1 前面的this.  和加上被去掉的static
    程序就能正確的運行了
    我發現產生這個問題的原因是因為每改變一下窗體的屬性 或者窗體內控件的屬性改變 或者刪除其他控件 或者新增控件的時候  會重新產生托放到窗體上控件(包括窗體自身)自動生成的代碼原來的代碼會被覆蓋掉.我就是要從別的類來操作static控件listBox1 ,請問你們大家 有誰知道怎麼做嗎?????????