158 lines
No EOL
6.7 KiB
C#
158 lines
No EOL
6.7 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using System.Drawing;
|
|
|
|
namespace ExtensionMethods
|
|
{
|
|
/// <summary>
|
|
/// <para>Add extensions to <c><see cref="ProgressBar"/></c></para>
|
|
/// </summary>
|
|
public static class ProgressBarExtensions
|
|
{
|
|
/// <summary>
|
|
/// <para>Advances the current position of the progress bar by the amount of the <see cref="ProgressBar.Step"/> property</para>
|
|
/// <para>Also this functions displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="showPercentage">If true: Display the percentage</param>
|
|
/// <param name="font">Font for the percentage text</param>
|
|
/// <param name="pointF">Point for the percentage text</param>
|
|
public static ProgressBar PerformStep(this ProgressBar pb, bool showPercentage, Font font, PointF pointF)
|
|
{
|
|
pb.PerformStep();
|
|
pb.Refresh();
|
|
if (showPercentage)
|
|
pb.PrintValue(font, pointF);
|
|
return pb;
|
|
}
|
|
/// <summary>
|
|
/// <para>Advances the current position of the progress bar by the amount of the <see cref="ProgressBar.Step"/> property</para>
|
|
/// <para>Also this functions displays the percentage in the middle of the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="showPercentage">If true: Display the percentage in the middle of the progress bar</param>
|
|
public static ProgressBar PerformStep(this ProgressBar pb, bool showPercentage)
|
|
{
|
|
return pb.PerformStep(showPercentage, new Font("Arial", (float)8.25, FontStyle.Regular), new PointF(pb.Width / 2 - 10, pb.Height / 2 - 7));
|
|
}
|
|
/// <summary>
|
|
/// <para>Advances the current position of the progress bar by the amount of the <see cref="ProgressBar.Step"/> property</para>
|
|
/// <para>Also this functions displays the percentage in the middle of the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="showPercentage">If true: Display the percentage in the middle of the progress bar</param>
|
|
/// <param name="font">Font for the percentage text</param>
|
|
public static ProgressBar PerformStep(this ProgressBar pb, bool showPercentage, Font font)
|
|
{
|
|
return pb.PerformStep(showPercentage, font, new PointF(pb.Width / 2 - 10, pb.Height / 2 - 7));
|
|
}
|
|
/// <summary>
|
|
/// <para>Advances the current position of the progress bar by the amount of the <see cref="ProgressBar.Step"/> property</para>
|
|
/// <para>Also this functions displays the percentage in the middle of the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="showPercentage">If true: Display the percentage in the middle of the progress bar</param>
|
|
/// <param name="pointF">Point for the percentage text</param>
|
|
public static ProgressBar PerformStep(this ProgressBar pb, bool showPercentage, PointF pointF)
|
|
{
|
|
return pb.PerformStep(showPercentage, new Font("Arial", (float)8.25, FontStyle.Regular), pointF);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the <see cref="ProgressBar.Maximum"/> to the specified <paramref name="Maximum"/> and reset the <paramref name="progressBar"/>.Value
|
|
/// </summary>
|
|
/// <param name="progressBar">ProgressBar</param>
|
|
/// <param name="Maximum">Maximum value of the range of the control</param>
|
|
/// <returns></returns>
|
|
public static ProgressBar SetMaximum(this ProgressBar progressBar, int Maximum)
|
|
{
|
|
progressBar.Maximum = Maximum * progressBar.Step;
|
|
progressBar.Reset();
|
|
return progressBar;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Sets the <see cref="ProgressBar.Value"/> to <see cref="ProgressBar.Minimum"/></para>
|
|
/// </summary>
|
|
/// <param name="progressBar"></param>
|
|
/// <returns></returns>
|
|
public static ProgressBar Reset(this ProgressBar progressBar)
|
|
{
|
|
progressBar.Value = progressBar.Minimum;
|
|
return progressBar;
|
|
}
|
|
/// <summary>
|
|
/// <para>Sets the <see cref="ProgressBar.Value"/> to <see cref="ProgressBar.Minimum"/></para>
|
|
/// </summary>
|
|
/// <param name="progressBar"></param>
|
|
/// <returns></returns>
|
|
public static ToolStripProgressBar Reset(this ToolStripProgressBar progressBar)
|
|
{
|
|
progressBar.Value = progressBar.Minimum;
|
|
return progressBar;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
public static ProgressBar PrintValue(this ProgressBar pb)
|
|
{
|
|
return pb.PrintValue(new Font("Arial", (float)8.25, FontStyle.Regular));
|
|
}
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="font"><see cref="Font"/> for the percentage text</param>
|
|
public static ProgressBar PrintValue(this ProgressBar pb, Font font)
|
|
{
|
|
int percent = (int)(((double)pb.Value / (double)pb.Maximum) * 100);
|
|
pb.CreateGraphics().DrawString(percent.ToString() + "%", font, Brushes.Black, new PointF(pb.Width / 2 - 10, pb.Height / 2 - 7));
|
|
return pb;
|
|
}
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="font"><see cref="Font"/> for the percentage text</param>
|
|
/// <param name="pointF"><see cref="PointF"/> for the percentage text</param>
|
|
public static ProgressBar PrintValue(this ProgressBar pb, Font font, PointF pointF)
|
|
{
|
|
int percent = (int)(((double)pb.Value / (double)pb.Maximum) * 100);
|
|
return pb.PrintText(percent.ToString() + "%", font, pointF);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="text">The text to print</param>
|
|
public static ProgressBar PrintText(this ProgressBar pb, string text)
|
|
{
|
|
return pb.PrintText(text, new Font("Arial", (float)8.25, FontStyle.Regular));
|
|
}
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="text">The text to print</param>
|
|
/// <param name="font"><see cref="Font"/> for the text</param>
|
|
public static ProgressBar PrintText(this ProgressBar pb, string text, Font font)
|
|
{
|
|
return pb.PrintText(text, font, new PointF(pb.Width / 2 - 10, pb.Height / 2 - text.Length*2));
|
|
}
|
|
/// <summary>
|
|
/// <para>Displays the percentage on the progress bar</para>
|
|
/// </summary>
|
|
/// <param name="pb"></param>
|
|
/// <param name="text">The text to print</param>
|
|
/// <param name="font"><see cref="Font"/> for the percentage text</param>
|
|
/// <param name="pointF"><see cref="PointF"/> for the percentage text</param>
|
|
public static ProgressBar PrintText(this ProgressBar pb, string text, Font font, PointF pointF)
|
|
{
|
|
pb.CreateGraphics().DrawString(text, font, Brushes.Black, pointF);
|
|
return pb;
|
|
}
|
|
}
|
|
} |