Search code examples
c#formslabeldelayshow

How to force Form2.label1.Text appear immediately when Form2.Show()?


I have this code:

namespace TuyenTk
{
    public partial class Form1 : Form
    {
        Form2 _form2 = new Form2("");
        public Form1()
        {
            InitializeComponent();
            _form2.Show();
            int i = 0;
            while (i < 5)
            {
                _form2.label1.Text = "" + i;
                Thread.Sleep(500);
                i++;
            }
        }
    }

    public class Form2 : Form
    {
        public System.Windows.Forms.Label label1;
        public System.ComponentModel.Container components = null;

        public Form2()
        {
            InitializeComponent();
        }
        private void InitializeComponent(string t)
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(5, 5);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(290, 100);
            this.label1.TabIndex = 0;
            this.label1.Text = t;
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(300, 100);
            this.ControlBox = false;
            this.Controls.Add(this.label1);
            this.Name = "Form2";
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.ShowInTaskbar = false;
            this.ResumeLayout(false);    
        }
    }
}

when Form1 run, it show the Form2, but the Form2.label1 background is white, and have no text.
After 2.5 seconds, Form2.label1.Text = 4. So the value 0, 1, 2, 3 of i dont appear. How I can fix it? Thank you very much.


Solution

  • What you want to do (periodically update label) is achieved by usage of Timer component (you can drag it from ToolBox and place on your form).

    public partial class Form1 : Form
    {
       Form2 _form2 = new Form2("");
       Timer _timer;
       int _counter;
    
       public Form1()
       {
           InitializeComponent();          
           _form2.Show();
    
           if (components == null)
               components = new System.ComponentModel.Container();
           _timer = new Timer(components); // required for correct disposing
    
           _timer.Interval = 500;
           _timer.Tick += timer_Tick;
           _timer.Start();
       }
    
       private void timer_Tick(object sender, EventArgs e)
       {
           if (_counter < 5)
           {
              _form2.label1.Text = _counter.ToString();
              _counter++;
              return;
           }
    
           _timer.Stop();
       }
    

    Also creating public controls on other forms is not very good idea - if you really need to update some value on form2, then its better to declare public method/property in Form2 class, which will update label:

    public partial class Form2 : Form
    {
         public int Value
         {             
             set { label1.Text = value.ToString(); }
         }
    }
    

    Also consider to move timer to Form2 (let this form update itself).