code_philo

导航

C# ToolStripProgressBar

ToolStripProgressBar 将所有 ToolStrip控件的移动和渲染功能和其典型的进程跟踪功能结合在一起。ToolStripProgressBar通常放在StatusStrip中,偶尔放在ToolStrip中。

ToolStripProgressBar通常在执行复制文件或打印文件等任务时使用,由于执行任务的时间较长,如果没有视觉提示,用户可能会任务应用程序没有响应。

ToolStripProgressBar的重要成员如下:

名称 说明
MarqueeAnimationSpeed 获取或设置Marquee每次更新进度之间的延迟(以毫秒为单位)
Maximum 进度条上限
Minimum 进度条下限
Style 进度条显示的样式
Value 进度条的当前值
PerformStep 根据Step属性值增加当前进度条值

Maximum和Minimum属性用于表示任务进度的最大和最小值。Minimum通常设置为0,Maximum通常为任务完成时的值。 例如,若要正确显示复制一组文件的进度,Maximum可以设置为需要复制的文件个数。Value属性表示当前的进度。

除了直接修改Value属性之外,还有许多方法可以修改由ToolStripProgressBar显示的值,如为Step属性指定Value每次递增的值,然后调用PerformStep使其递增。如果要修改递增的值,可以用Increment方法。

示例

下面的代码通过计算Fibonacci数列演示ToolStripProgressBar

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();
    }
}

界面如下:

image

posted on 2015-03-12 15:58  code_philosophy  阅读(5520)  评论(0编辑  收藏  举报