C# 如何在WinForm标签中创建运行文本?(如photoshop加载页面)
如果你打开adobe photo shop,一个小窗口会突然打开。您可以看到正在运行的文本初始化。。。阅读字体。。像那样 我也喜欢在我的项目中运行这种类型的文本 我试着做个循环。但它不显示C# 如何在WinForm标签中创建运行文本?(如photoshop加载页面),c#,winforms,C#,Winforms,如果你打开adobe photo shop,一个小窗口会突然打开。您可以看到正在运行的文本初始化。。。阅读字体。。像那样 我也喜欢在我的项目中运行这种类型的文本 我试着做个循环。但它不显示 for (int j = 1; j <= 3; j++) { label1.Text = "Please Wait."; label1.Text = "Please Wait..";
for (int j = 1; j <= 3; j++)
{
label1.Text = "Please Wait.";
label1.Text = "Please Wait..";
label1.Text = "Please Wait...";
label1.Text = "Please Wait.";
label1.Text = "Please Wait..";
label1.Text = "Please Wait...";
label1.Text = "Please Wait.";
label1.Text = "Please Wait..";
label1.Text = "Please Wait...";
label1.Text = "Please Wait.";
label1.Text = "Please Wait..";
label1.Text = "Please Wait...";
label1.Text = "Please Wait.";
label1.Text = "Please Wait..";
label1.Text = "Please Wait...";
}
for(int j=1;j您需要在两者之间添加间隔,否则除了最后一个,您将看不到文本。
您还需要更新GUI,如下面Barfieldmv的评论所示,如下所示:
For(int i =0; i<3;i++)
{
label1.Text = "Please Wait.";
label1.Update();
system.Threading.Thread.Sleep(500);
label1.Text = "Please Wait..";
label1.Update();
system.Threading.Thread.Sleep(500);
label1.Text = "Please Wait...";
}
For(int i=0;i问题在于您正在阻止UI线程。应用程序是单线程的,这意味着无论您执行什么操作,应用程序都不会重新绘制UI元素
如果您有过多的任务要做,您应该始终在单独的线程上执行这些任务,以确保UI保持响应。这可以通过f.e.建立。它将在另一个线程上执行任务,并将继续向UI线程发送更新,而不会阻塞它或减慢实际工作
不知羞耻地从MSDN页面复制并由我修改:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace BackgroundWorkerSimple
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void startAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void cancelAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
}
}
// This event handler is where the time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// Excessive comuptation goes here, you can report back via this:
worker.ReportProgress(progressInPercent, additionalProgressAsObject);
}
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
}
// This event handler deals with the results of the background operation.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
resultLabel.Text = "Canceled!";
}
else if (e.Error != null)
{
resultLabel.Text = "Error: " + e.Error.Message;
}
else
{
resultLabel.Text = "Done!";
}
}
}
}
太长了,读不下去了。有很多重复的东西,都是这样的:很多,更多,TL;DR停止阻塞UI线程。你正在快速地改变文本。尝试抛出一个线程。睡眠(100);Apple。doEvsTo();在你试图设置文本的每个地方之间。(int j=1;j为什么大家都在这里发表评论?。在回答部分发布这些东西有什么问题?有什么特别的原因吗?.bz它只有很少的空间!。它们之间需要“非”阻塞间隔。否则,您只会看到自上次UI更新以来的最后一个文本。为“更新”文本添加计时器会给UI刷新屏幕的时间。它的工作原理是:k在按钮点击事件中。但我希望它在窗口加载事件中。可能吗?!。将代码放在当时显示的表格中。它可以工作,但您真正想要的是一个体面的启动屏幕,请参阅: