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 计算斐波纳契数序列的 。
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 表示应用程序完成操作的进度。 由于 控件中显示的条形图是块的集合,因此 显示 ToolStripProgressBar 的值仅近似 Value 于 属性的当前值。 根据 的大小 ToolStripProgressBar, Value 属性确定何时显示下一个块。
除了直接更改 Value 属性之外,有多种方法可以修改 由 ToolStripProgressBar 显示的值。 可以使用 Step 属性指定特定值以递增 Value 属性,然后调用 PerformStep 方法来递增值。 若要更改递增值,可以使用 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) |
引发 |