ToolStripProgressBar 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示 StatusStrip 中所包含的 Windows 進度列控制項。
public ref class ToolStripProgressBar : System::Windows::Forms::ToolStripControlHost
public class ToolStripProgressBar : System.Windows.Forms.ToolStripControlHost
type ToolStripProgressBar = class
inherit ToolStripControlHost
Public Class ToolStripProgressBar
Inherits ToolStripControlHost
- 繼承
- 繼承
-
ToolStripProgressBar
範例
下列程式碼範例示範 ToolStripProgressBar 計算 Fibonacci 數位序列的 。
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();
}
}
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.ComponentModel
Class FibonacciNumber
Inherits Form
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New FibonacciNumber())
End Sub
Private progressStatusStrip As StatusStrip
Private toolStripProgressBar As ToolStripProgressBar
Private requestedCountControl As NumericUpDown
Private goButton As Button
Private outputTextBox As TextBox
Private backgroundWorker As BackgroundWorker
Private toolStripStatusLabel As ToolStripStatusLabel
Private requestedCount As Integer
Public Sub New()
[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)
Dim flp As New FlowLayoutPanel()
flp.Dock = DockStyle.Top
Dim beforeLabel As 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)
Dim afterLabel As New Label()
afterLabel.Text = "Numbers in the Fibonacci sequence."
afterLabel.AutoSize = True
flp.Controls.Add(afterLabel)
goButton = New Button()
goButton.Text = "&Go"
AddHandler goButton.Click, AddressOf 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
AddHandler backgroundWorker.DoWork, AddressOf backgroundWorker1_DoWork
AddHandler backgroundWorker.RunWorkerCompleted, AddressOf backgroundWorker1_RunWorkerCompleted
AddHandler backgroundWorker.ProgressChanged, AddressOf backgroundWorker1_ProgressChanged
End Sub
Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
' 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...")
Dim lastlast As [Decimal] = 0
Dim last As [Decimal] = 1
Dim current As [Decimal]
If requestedCount >= 1 Then
AppendNumber(0)
End If
If requestedCount >= 2 Then
AppendNumber(1)
End If
Dim i As Integer
While i < requestedCount
' Calculate the number.
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
i += 1
End While
backgroundWorker.ReportProgress(100, "Complete!")
End Sub
Delegate Sub AppendNumberDelegate(number As [Decimal])
Private Sub AppendNumber(number As [Decimal])
If outputTextBox.InvokeRequired Then
outputTextBox.Invoke(New AppendNumberDelegate(AddressOf AppendNumber), number)
Else
outputTextBox.AppendText((number.ToString("N0") + Environment.NewLine))
End If
End Sub
Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
toolStripProgressBar.Value = e.ProgressPercentage
toolStripStatusLabel.Text = e.UserState '
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
If TypeOf e.Error Is OverflowException Then
outputTextBox.AppendText((Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**"))
End If
toolStripProgressBar.Enabled = False
requestedCountControl.Enabled = True
goButton.Enabled = True
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
goButton.Enabled = False
toolStripProgressBar.Enabled = True
requestedCount = Fix(requestedCountControl.Value)
requestedCountControl.Enabled = False
outputTextBox.Clear()
backgroundWorker.RunWorkerAsync()
End Sub
End Class
備註
ToolStripProgressBar 是針對 ProgressBar 在 中 ToolStrip 裝載優化的 。 裝載控制項的屬性和事件的子集會在層級公開 ToolStripProgressBar ,但基礎 ProgressBar 控制項可透過 ProgressBar 屬性完全存取。
ToolStripProgressBar控制項會以視覺化方式指出長時間作業的進度。 控制項 ToolStripProgressBar 會顯示從左至右填入的橫條,並顯示系統在作業進行時反白顯示色彩。
注意
控制項 ToolStripProgressBar 只能水準方向。
ToolStripProgressBar當應用程式執行複製檔案或列印檔案等工作時,通常會使用控制項。 如果沒有視覺提示,應用程式的使用者可能會考慮應用程式沒有回應。 ToolStripProgressBar使用 來通知使用者應用程式正在執行冗長的工作,且應用程式仍在回應中。
Maximum和 Minimum 屬性會定義值範圍,以代表工作的進度。 屬性 Minimum 通常會設定為零的值,而且 Maximum 屬性通常會設定為值,指出工作完成。 例如,若要在複製檔案群組時正確顯示進度, Maximum 屬性可以設定為要複製的檔案總數。 屬性 Value 代表應用程式完成作業的進度。 因為控制項中顯示的列是區塊集合,所以唯 Value 一由 ToolStripProgressBar 屬性目前的值所顯示的值。 根據 的大小 ToolStripProgressBar , Value 屬性會決定何時要顯示下一個區塊。
除了直接變更 Value 屬性以外,還有幾種方式可以修改 所顯示 ToolStripProgressBar 的值。 您可以使用 Step 屬性來指定要遞增屬性的特定值,然後呼叫 PerformStep 方法來遞增 Value 值。 若要改變遞增值,您可以使用 Increment 方法,並指定要遞增 Value 屬性的值。
ToolStripProgressBar 會取代舊 ProgressBar 版控制項,但保留為回溯相容性。
建構函式
ToolStripProgressBar() |
初始化 ToolStripProgressBar 類別的新執行個體。 |
ToolStripProgressBar(String) |
使用指定的名稱,初始化 ToolStripProgressBar 類別的新執行個體。 |
屬性
方法
事件
明確介面實作
IDropTarget.OnDragDrop(DragEventArgs) |
引發 DragDrop 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragEnter(DragEventArgs) |
引發 DragEnter 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragLeave(EventArgs) |
引發 DragLeave 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragOver(DragEventArgs) |
引發 |