如何在WinForm Label中创建运行文本? (如photoshop加载页面)

时间:2011-06-03 11:03:14

标签: c# winforms

enter image description here

如果你打开adobe照相馆,一个小窗口突然打开。在那你可以看到正在运行的文本Initilizing ...读取字体..就像那样。

我喜欢在我的项目中执行此类运行文本..

我尝试进入循环。但它没有显示!。

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...";
            }

请提出建议。

2 个答案:

答案 0 :(得分:1)

您需要在两者之间添加间隔,否则您将看不到除最后一个之外的文本。 您还需要像下面Barfieldmv的评论那样更新GUI,如下所示:

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...";
}

答案 1 :(得分:0)

问题是您正在阻止UI线程。该应用程序是单线程的,这意味着无论您执行什么操作都会使应用程序不重新绘制UI元素。

如果您有过多的任务要做,您应该始终在单独的线程上执行它们,以确保UI保持响应。这可以使用BackGroundWorker 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!";
            }
        }
    }
}