C # ToolStripProgressBar,

Source: Internet
Author: User

C # ToolStripProgressBar,

ToolStripProgressBar combines the movement and rendering functions of all ToolStrip controls with their typical process tracking functions. ToolStripProgressBar is usually placed in StatusStrip and occasionally in ToolStrip.

ToolStripProgressBar is usually used when you execute a copy or print file task. Because the task has been executed for a long time, if there is no visual prompt, the task application may not respond.

Important ToolStripProgressBar members are as follows:

Name Description
MarqueeAnimationSpeed Gets or sets the latency (in milliseconds) between each update progress of Marquee)
Maximum Upper Limit of progress bar
Minimum Lower limit of progress bar
Style Display style of progress bar
Value Current Value of the progress bar
PerformStep Add the current progress bar value based on the Step property value

The Maximum and Minimum attributes indicate the Maximum and Minimum values of the task progress. Minimum is usually set to 0, and Maximum is usually the value when the task is completed. For example, to correctly display the progress of copying a group of files, Maximum can be set to the number of files to be copied. The Value Attribute indicates the current progress.

In addition to directly modifying the Value attribute, there are also many ways to modify the Value displayed by the ToolStripProgressBar. For example, you can specify the Value to increase each time for the Step attribute, and then call PerformStep to increase it. If you want to modify the incremental value, you can use the Increment method.

Example

The following code demonstrates ToolStripProgressBar by calculating the Fibonacci series

using System;using System.Collections.Generic;using System.Windows.Forms;using System.ComponentModel;class FibonacciNumber : Form{    [STAThread]    static void Main()    {        Application.EnableVisualStyles();        Application.Run(new FibonacciNumber());    }    private StatusStrip progressStatusStrip;    private ToolStripProgressBar toolStripProgressBar;    private NumericUpDown requestedCountControl;    private Button goButton;    private TextBox outputTextBox;    private BackgroundWorker backgroundWorker;    private ToolStripStatusLabel toolStripStatusLabel;    private int requestedCount;    public FibonacciNumber()    {        Text = "Fibonacci";                // Prepare the StatusStrip.        progressStatusStrip = new StatusStrip();        toolStripProgressBar = new ToolStripProgressBar();        toolStripProgressBar.Enabled = false;        toolStripStatusLabel = new ToolStripStatusLabel();        progressStatusStrip.Items.Add(toolStripProgressBar);        progressStatusStrip.Items.Add(toolStripStatusLabel);        FlowLayoutPanel flp = new FlowLayoutPanel();        flp.Dock = DockStyle.Top;        Label beforeLabel = new Label();        beforeLabel.Text = "Calculate the first ";        beforeLabel.AutoSize = true;        flp.Controls.Add(beforeLabel);        requestedCountControl = new NumericUpDown();        requestedCountControl.Maximum = 1000;        requestedCountControl.Minimum = 1;        requestedCountControl.Value = 100;        flp.Controls.Add(requestedCountControl);        Label afterLabel = new Label();        afterLabel.Text = "Numbers in the Fibonacci sequence.";        afterLabel.AutoSize = true;        flp.Controls.Add(afterLabel);                goButton = new Button();        goButton.Text = "&Go";        goButton.Click += new System.EventHandler(button1_Click);        flp.Controls.Add(goButton);        outputTextBox = new TextBox();        outputTextBox.Multiline = true;        outputTextBox.ReadOnly = true;        outputTextBox.ScrollBars = ScrollBars.Vertical;        outputTextBox.Dock = DockStyle.Fill;        Controls.Add(outputTextBox);        Controls.Add(progressStatusStrip);        Controls.Add(flp);        backgroundWorker = new BackgroundWorker();        backgroundWorker.WorkerReportsProgress = true;        backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);        backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);            }    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)    {        // This method will run on a thread other than the UI thread.        // Be sure not to manipulate any Windows Forms controls created        // on the UI thread from this method.        backgroundWorker.ReportProgress(0, "Working...");        Decimal lastlast = 0;        Decimal last = 1;        Decimal current;        if (requestedCount >= 1)        { AppendNumber(0); }        if (requestedCount >= 2)        { AppendNumber(1); }        for (int i = 2; i < requestedCount; ++i)        {            // Calculate the number.            checked { current = lastlast + last; }            // Introduce some delay to simulate a more complicated calculation.            System.Threading.Thread.Sleep(100);            AppendNumber(current);            backgroundWorker.ReportProgress((100 * i) / requestedCount, "Working...");            // Get ready for the next iteration.            lastlast = last;            last = current;        }        backgroundWorker.ReportProgress(100, "Complete!");    }    private delegate void AppendNumberDelegate(Decimal number);    private void AppendNumber(Decimal number)    {        if (outputTextBox.InvokeRequired)        { outputTextBox.Invoke(new AppendNumberDelegate(AppendNumber), number); }        else        { outputTextBox.AppendText(number.ToString("N0") + Environment.NewLine); }    }    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)    {        toolStripProgressBar.Value = e.ProgressPercentage;        toolStripStatusLabel.Text = e.UserState as String;    }    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)    {        if (e.Error is OverflowException)        { outputTextBox.AppendText(Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**"); }        toolStripProgressBar.Enabled = false;        requestedCountControl.Enabled = true;        goButton.Enabled = true;    }    private void button1_Click(object sender, EventArgs e)    {        goButton.Enabled = false;        toolStripProgressBar.Enabled = true;        requestedCount = (int)requestedCountControl.Value;        requestedCountControl.Enabled = false;        outputTextBox.Clear();        backgroundWorker.RunWorkerAsync();    }}

The interface is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.