开发者

How to create runing text in WinForm Label? (like photoshop loading page)

How to create runing text in WinForm Label? (like photoshop loading page)

If you open adobe photo shop, one small window is suddenly open. In that you can saw the running text Initilizing... Reading Fonts.. like that.

I like to do this type of running text in my project also..

I try to in for loop. but it not show!.

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."开发者_JAVA百科;
                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...";
            }

pls give a suggestion.,


you need to add intervals in between, otherwise you won't see the text except the last one. You'll also need to update the GUI as in Barfieldmv's comments below, so something like this:

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


The problem is that you're blocking the UI thread. The application is Single-Threaded, that means that whatever you perform will keep the application from redrawing the UI elements.

If you have excessive tasks to do, you should always perform them on a separate thread to ensure that the UI stays responsive. This can be established with a BackGroundWorker f.e.. It will perform the tasks on another thread and will keep sending updates to the UI thread without blocking it or slowing down the real work.

Shamelessly copied from the MSDN page and modified by me:

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!";
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜