C# 无法更新提示框中的标签
我正在编写以下代码:C# 无法更新提示框中的标签,c#,multithreading,winforms,visual-studio-2010,user-interface,C#,Multithreading,Winforms,Visual Studio 2010,User Interface,我正在编写以下代码: private Label textLabel; public void ShowDialog() { Form prompt = new Form(); prompt.Width = 500; prompt.Height = 150; prompt.Text = caption; textLabel = new Label() { Le
private Label textLabel;
public void ShowDialog()
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.Text = caption;
textLabel = new Label() { Left = 50, Top=20, Text="txt"};
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.ShowDialog();
}
我正在使用另一个方法调用上述方法,并尝试更新循环中的textLabel
字段,如下所示
public void doIt()
{
ShowDialog();
for(int i=0;i<10;i++)
{
textLabel.TEXT = ""+i;
Threading.Thread.Sleep(1000);
}
}
public void doIt()
{
ShowDialog();
对于(int i=0;i,我将这样做,这不是一个完整的解决方案,但我希望它将为您指明正确的方向:
创建将从表单派生的提示符
类。将控件添加到该类中(我手动完成,但您可以使用设计器)。添加计时器
,该计时器将每秒触发,并将更改标签的文本。当计数器达到10时,停止计时器
public partial class Prompt : Form
{
Timer timer;
Label textLabel;
TextBox textBox;
Button confirmation;
int count = 0;
public Prompt()
{
InitializeComponent();
this.Load += Prompt_Load;
this.Width = 500;
this.Height = 150;
textLabel = new Label() { Left = 50, Top = 20, Text = "txt" };
textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
this.Controls.Add(confirmation);
this.Controls.Add(textLabel);
this.Controls.Add(textBox);
timer = new Timer();
timer.Interval = 1000;
timer.Tick += timer_Tick;
}
void Prompt_Load(object sender, EventArgs e)
{
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
this.textLabel.Text = " " + count.ToString();
count++;
if (count == 10)
timer.Stop();
}
}
在doIt
方法中,创建Prompt
表单的实例,设置其标题并调用其ShowDialog()
方法
public void doIt()
{
Prompt p = new Prompt();
p.Text = caption;
p.ShowDialog();
}
因此,我将这样做,这不是一个完整的解决方案,但我希望它将为您指明正确的方向:
创建将从表单派生的提示符
类。将控件添加到该类中(我手动完成,但您可以使用设计器)。添加计时器
,该计时器将每秒触发,并将更改标签的文本。当计数器达到10时,停止计时器
public partial class Prompt : Form
{
Timer timer;
Label textLabel;
TextBox textBox;
Button confirmation;
int count = 0;
public Prompt()
{
InitializeComponent();
this.Load += Prompt_Load;
this.Width = 500;
this.Height = 150;
textLabel = new Label() { Left = 50, Top = 20, Text = "txt" };
textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
this.Controls.Add(confirmation);
this.Controls.Add(textLabel);
this.Controls.Add(textBox);
timer = new Timer();
timer.Interval = 1000;
timer.Tick += timer_Tick;
}
void Prompt_Load(object sender, EventArgs e)
{
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
this.textLabel.Text = " " + count.ToString();
count++;
if (count == 10)
timer.Stop();
}
}
在doIt
方法中,创建Prompt
表单的实例,设置其标题并调用其ShowDialog()
方法
public void doIt()
{
Prompt p = new Prompt();
p.Text = caption;
p.ShowDialog();
}
ShowDialog()是模态的,因此它会阻止代码运行,直到窗体关闭。我会使用计时器而不是for…循环/睡眠…或BackgroundWorker,换句话说,textLabel不可访问(超出范围)内行method@NikolaDavidovic:它被定义为一个全局变量虽然它是全局变量,但它并不是一个好方法,但我认为这不是一个好方法。最好的方法是创建一个单独的表单类,其中包含所需的控件,这些控件将使用指定的计时器(如@LarsTech)更新其textLabel。ShowDialog()是模态的,因此它会阻止代码运行,直到窗体关闭。我会使用计时器,而不是for…loop/sleep…或BackgroundWorker,换句话说,textLabel不可访问(超出范围)内行method@NikolaDavidovic:它被定义为一个全局变量虽然它是全局变量,但它并不是一个好方法,但我认为这不是一个好方法。最好的方法是创建一个带有所需控件的单独表单类,该类将使用计时器(如@LarsTech)更新其textLabel指定。抱歉耽搁。我非常感谢您的帮助。谢谢:)抱歉耽搁。我非常感谢您的帮助。谢谢:)