开发者

C# - Update textbox on forms created in code

I have a main form called mainForm - this runs my entire app. In this form I create other forms like this:

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

  Form[] formMessage = new Form[10];

  int formNumber =开发者_JAVA百科 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
}

I use the FNeventRigger to create my form, and I can have upto 10 of them open at each given time - I use this for showing count down timers.

The problem I have is how do I show the count down timer of each form? If I use : this.lblTimer.Text = newText, then only the newest form that was opened displays the correct timer.... the other forms lblTimer.Text stop functioning.

Is there a way to address all the lblTimer.Text on all forms opened on the array?

Thanks,


Create your own form class which defines the Label an a method to update this method. and then initiate all your forms using this new MyBaseForm

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜