0

私はこのコードを持っています:

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);    
        }
    }
}

Form1 を実行すると、Form2 が表示されますが、Form2.label1 の背景は白で、テキストはありません。
2.5 秒後、Form2.label1.Text = 4 です。したがって、i の値 0、1、2、3 は表示されません。どうすれば修正できますか?どうもありがとうございました。

4

2 に答える 2

3

あなたがしたいこと(定期的にラベルを更新する)は、タイマーコンポーネントを使用することによって達成されます(ツールボックスからドラッグしてフォームに配置できます)。

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();
   }

また、他のフォームでパブリックコントロールを作成することは、あまり良い考えではありません。form2で値を更新する必要がある場合は、Form2クラスでパブリックメソッド/プロパティを宣言することをお勧めします。これにより、ラベルが更新されます。

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

また、タイマーをForm2に移動することを検討してください(このフォーム自体を更新させてください)。

于 2012-12-08T14:56:00.473 に答える
0

Thread.Sleep(500);UIスレッドで呼び出すと、GUIは無責任になります。これが、Fomr2.label1の背景を白にする理由です。コードを移動することをお勧めします

while (i < 5)
{
   _form2.label1.Text = "" + i;
   Thread.Sleep(500);
   i++;
}

別のスレッドに。そして、あなたはあなたの目標を達成するためにこのリンクを参照することができます。

于 2012-12-08T14:54:06.713 に答える