【发布时间】:2012-12-08 14:40:04
【问题描述】:
我有这个代码:
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 不会出现。我该如何解决?非常感谢。
【问题讨论】:
-
添加 _form2.label1.Update();将是一种解决方法。不要写这样的代码,休眠 UI 线程总是是个坏主意。