当前位置: 首页>>代码示例>>C#>>正文


C# ToolStripProgressBar类代码示例

本文整理汇总了C#中System.Windows.Forms.ToolStripProgressBar的典型用法代码示例。如果您正苦于以下问题:C# ToolStripProgressBar类的具体用法?C# ToolStripProgressBar怎么用?C# ToolStripProgressBar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ToolStripProgressBar类属于System.Windows.Forms命名空间,在下文中一共展示了ToolStripProgressBar类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
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();
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:137,代码来源:ToolStripProgressBar


注:本文中的System.Windows.Forms.ToolStripProgressBar类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。