2009-10-26 63 views
1

我有一個名爲mainForm的主窗體 - 它運行我的整個應用程序。 在這種形式下,我創建其他形式是這樣的:C# - 在代碼中創建的窗體上更新文本框

...... 
...... 

    Form[] formMessage = new Form[10]; 

    int formNumber = 0;  

    System.Windows.Forms.Button btnCancel; 
    System.Windows.Forms.Button btnClose; 
    System.Windows.Forms.Label lblTimer; 
    System.Windows.Forms.Button btnOK; 
    System.Windows.Forms.Panel panel1; 
    System.Windows.Forms.Label lblMessage; 
    System.Windows.Forms.PictureBox pictureBox1; 
    System.Windows.Forms.Label lblTitle; 

...... 
...... 
public void CreateForm(Form form2) 
    { 
     this.btnCancel = new System.Windows.Forms.Button(); 
     this.btnClose = new System.Windows.Forms.Button(); 
     this.lblTimer = new System.Windows.Forms.Label(); 
     this.btnOK = new System.Windows.Forms.Button(); 
     this.panel1 = new System.Windows.Forms.Panel(); 
     this.lblMessage = new System.Windows.Forms.Label(); 
     this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
     this.lblTitle = new System.Windows.Forms.Label(); 
     // 
........ 
     // 
     // lblTimer 
     // 
     this.lblTimer.AutoSize = true; 
     this.lblTimer.BackColor = System.Drawing.Color.Transparent; 
     this.lblTimer.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     this.lblTimer.Location = new System.Drawing.Point(9, 120); 
     this.lblTimer.Name = "lblTimer"; 
     this.lblTimer.Size = new System.Drawing.Size(0, 16); 
     this.lblTimer.Visible = Show_Timer; 
     this.lblTimer.TabIndex = 4; 

     form2.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     form2.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     form2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))); 
     form2.ClientSize = new System.Drawing.Size(400, 142); 
     form2.ControlBox = false; 

     form2.Controls.Add(this.pictureBox1); 
     form2.Controls.Add(this.panel1); 
     form2.Controls.Add(this.btnOK); 
     form2.Controls.Add(this.lblTimer); 
     form2.Controls.Add(this.btnCancel); 


     form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
     form2.Opacity = 0.98; 
     form2.ShowInTaskbar = false; 
     form2.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
     form2.Tag = formNumber; 
     form2.Paint += new System.Windows.Forms.PaintEventHandler(this.MyMessageBox_Paint);  

    } 


private void FNeventTrigger(System.Object sender, System.EventArgs e) 
{ 

    formMessage[formNumber] = new Form(); 
    CreateForm(formMessage[formNumber]); 
    formMessage[formNumber].Show(); 
    formNumber++; 
    if (formNumber == 10) 
     formNumber = 0; 
} 

public void lbl_timer_UpdateText() 
{ 
    this.lblTimer.Text = newText 
} 

我用FNeventRigger創建自己的狀態,我可以每個給定時間有高達其中10開 - 我用這個顯示倒計時定時器。

我現在的問題是如何顯示每個窗體的倒數計時器? 如果我使用:this.lblTimer.Text = newText,那麼只有打開的最新窗體顯示正確的計時器....其他形式lblTimer.Text停止運作。

有沒有辦法解決在數組上打開的所有窗體上的所有lblTimer.Text?

感謝,

回答

1

創建自己的表單類定義了標籤的方法來更新這個方法。 ,然後開始使用這種新的MyBaseForm

public MyBaseForm : Form 
{ 
    private Label lblTimer; 
    public MyBaseForm() 
    { 
     lblTimer = new Label(); 
     Controls.Add(lblTimer); 
    } 
    public void UpdateTimerText(string text) 
    { 
     lblTimer.Text = text; 
    } 
} 
+0

你能否提供更多的細節你所有的形式,因爲我不知道如何實現這一(只被編程幾個星期.....) – JayT 2009-10-26 10:00:22

+0

基本上你試圖做的是創建一個新類型的表單,它具有可以從外部更新的標籤,這在標準Form類中不受支持,所以您需要通過繼承它來擴展Form類,這就是MyBaseForm類的確如此,所以在你創建這個類之後,你更新你的代碼並使用這個類而不是Form類來創建你的表單列表 – bashmohandes 2009-10-26 10:32:06

相關問題