程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# ToolStripProgressBar,

C# ToolStripProgressBar,

編輯:C#入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved