我正在尝试更新标签文字。当原始文本包含多行时,我将文本更新为具有单行的内容,即使文本在顶行更新,旧的多行仍然可见。这是一个例子。我该如何解决这个问题?
PhotoText.Text = "Testing Some\nCode Out\nRight Now!"; //This shows 3 lines
PhotoText.Refresh();
Thread.Sleep(1000);
PhotoText.Text = "New Text!"; //This shows 1 lines
PhotoText.Refresh();
Thread.Sleep(1000);
答案 0 :(得分:1)
您不应该阻止UI线程。分别运行长时间运行的任务:
private void button46_Click(object sender, EventArgs ee)
{
new Task(() =>
{
this.Invoke(new EventHandler((o, e) => PhotoText.Text = "Testing Some\nCode Out\nRight Now!"));
Thread.Sleep(1000);
this.Invoke(new EventHandler((o, e) => PhotoText.Text = "New Text!"));
Thread.Sleep(1000);
}, TaskCreationOptions.LongRunning).Start();
}
this
是一个表单,Invoke
在UI线程上运行PhotoText.Text =...
。即使没有this.BeginInvoke
(see more),您也可以使用EndInvoke
。
答案 1 :(得分:-2)
尝试致电
this.Refresh();
Application.DoEvents();
每次更新之间。
答案 2 :(得分:-3)
尝试使用字符串
PhotoText.Text = "Testing Some\nCode Out\nRight Now!"; //This shows 3 lines
PhotoText.Refresh();
Thread.Sleep(1000);
string phototext="New Text!";
PhotoText.Text = phototext; //This shows 1 lines
PhotoText.Refresh();
Thread.Sleep(1000);