using System; using System.Windows.Forms; using System.Drawing; namespace ExtensionMethods { /// /// Add extensions to /// public static class ProgressBarExtensions { /// /// Advances the current position of the progress bar by the amount of the property /// Also this functions displays the percentage on the progress bar /// /// /// If true: Display the percentage /// Font for the percentage text /// Point for the percentage text 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; } /// /// Advances the current position of the progress bar by the amount of the property /// Also this functions displays the percentage in the middle of the progress bar /// /// /// If true: Display the percentage in the middle of the progress bar 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)); } /// /// Advances the current position of the progress bar by the amount of the property /// Also this functions displays the percentage in the middle of the progress bar /// /// /// If true: Display the percentage in the middle of the progress bar /// Font for the percentage text 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)); } /// /// Advances the current position of the progress bar by the amount of the property /// Also this functions displays the percentage in the middle of the progress bar /// /// /// If true: Display the percentage in the middle of the progress bar /// Point for the percentage text public static ProgressBar PerformStep(this ProgressBar pb, bool showPercentage, PointF pointF) { return pb.PerformStep(showPercentage, new Font("Arial", (float)8.25, FontStyle.Regular), pointF); } /// /// Set the to the specified and reset the .Value /// /// ProgressBar /// Maximum value of the range of the control /// public static ProgressBar SetMaximum(this ProgressBar progressBar, int Maximum) { progressBar.Maximum = Maximum * progressBar.Step; progressBar.Reset(); return progressBar; } /// /// Sets the to /// /// /// public static ProgressBar Reset(this ProgressBar progressBar) { progressBar.Value = progressBar.Minimum; return progressBar; } /// /// Sets the to /// /// /// public static ToolStripProgressBar Reset(this ToolStripProgressBar progressBar) { progressBar.Value = progressBar.Minimum; return progressBar; } /// /// Displays the percentage on the progress bar /// /// public static ProgressBar PrintValue(this ProgressBar pb) { return pb.PrintValue(new Font("Arial", (float)8.25, FontStyle.Regular)); } /// /// Displays the percentage on the progress bar /// /// /// for the percentage text 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; } /// /// Displays the percentage on the progress bar /// /// /// for the percentage text /// for the percentage text 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); } /// /// Displays the percentage on the progress bar /// /// /// The text to print public static ProgressBar PrintText(this ProgressBar pb, string text) { return pb.PrintText(text, new Font("Arial", (float)8.25, FontStyle.Regular)); } /// /// Displays the percentage on the progress bar /// /// /// The text to print /// for the text 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)); } /// /// Displays the percentage on the progress bar /// /// /// The text to print /// for the percentage text /// for the percentage text public static ProgressBar PrintText(this ProgressBar pb, string text, Font font, PointF pointF) { pb.CreateGraphics().DrawString(text, font, Brushes.Black, pointF); return pb; } } }