2009-10-26 64 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

相关问题