it was working fine first time when i dragged the custom progressbar to the form1 designer from the toolbox. but now each time i try to drag it i get the exception error message.
and i didn’t change anything.
link for the code where i took it from.
https://github.com/ukushu/TextProgressBar/blob/master/TextProgressBar.cs
here is an image screenshot showing the error message:
and the code
protected override void OnPaint(PaintEventArgs e)
{
if (e.Graphics == null)
{
return;
}
Graphics g = e.Graphics;
DrawProgressBar(g);
DrawStringIfNeeded(g);
}
line 134 is:
DrawStringIfNeeded(g);
and
private void DrawStringIfNeeded(Graphics g)
{
if (VisualMode != ProgressBarDisplayMode.NoText)
{
string text = _textToDraw;
if (!string.IsNullOrEmpty(text))
{
SizeF len = g.MeasureString(text, TextFont);
Point location = new Point((Width / 2) - (int)len.Width / 2, (Height / 2) - (int)len.Height / 2);
g.DrawString(text, TextFont, _textColourBrush, location);
}
}
}
line 165 is:
g.DrawString(text, TextFont, _textColourBrush, location);
and the full code , it’s a bit long but might be needed:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public enum ProgressBarDisplayMode
{
NoText,
Percentage,
CurrProgress,
CustomText,
TextAndPercentage,
TextAndCurrProgress
}
public class CustomProgressBar : ProgressBar
{
[Description("Font of the text on ProgressBar"), Category("Additional Options")]
public Font TextFont { get; set; } = new Font(FontFamily.GenericSerif, 14, FontStyle.Bold | FontStyle.Italic);
private SolidBrush _textColourBrush = (SolidBrush)Brushes.Black;
[Category("Additional Options")]
public Color TextColor
{
get
{
return _textColourBrush.Color;
}
set
{
_textColourBrush.Dispose();
_textColourBrush = new SolidBrush(value);
}
}
private SolidBrush _progressColourBrush = (SolidBrush)Brushes.LightGreen;
[Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public Color ProgressColor
{
get
{
return _progressColourBrush.Color;
}
set
{
_progressColourBrush.Dispose();
_progressColourBrush = new SolidBrush(value);
}
}
private ProgressBarDisplayMode _visualMode = ProgressBarDisplayMode.Percentage;
[Category("Additional Options"), Browsable(true)]
public ProgressBarDisplayMode VisualMode
{
get
{
return _visualMode;
}
set
{
_visualMode = value;
Invalidate(); // Redraw component after change value from VS Properties section
}
}
private string _text = string.Empty;
[Description("If it's empty, % will be shown"), Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string CustomText
{
get
{
return _text;
}
set
{
_text = value;
Invalidate(); // Redraw component after change value from VS Properties section
}
}
private string _textToDraw
{
get
{
string text = CustomText;
switch (VisualMode)
{
case ProgressBarDisplayMode.Percentage:
text = _percentageStr;
break;
case ProgressBarDisplayMode.CurrProgress:
text = _currProgressStr;
break;
case ProgressBarDisplayMode.TextAndCurrProgress:
text = $"{CustomText}: {_currProgressStr}";
break;
case ProgressBarDisplayMode.TextAndPercentage:
text = $"{CustomText}: {_percentageStr}";
break;
}
return text;
}
}
private string _percentageStr => $"{(int)((float)Value - Minimum) / ((float)Maximum - Minimum) * 100} %";
private string _currProgressStr => $"{Value}/{Maximum}";
public CustomProgressBar()
{
Value = Minimum;
FixComponentBlinking();
}
private void FixComponentBlinking()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (e.Graphics == null)
{
return;
}
Graphics g = e.Graphics;
DrawProgressBar(g);
DrawStringIfNeeded(g);
}
private void DrawProgressBar(Graphics g)
{
Rectangle rect = ClientRectangle;
ProgressBarRenderer.DrawHorizontalBar(g, rect);
rect.Inflate(-3, -3);
if (Value > 0)
{
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
g.FillRectangle(_progressColourBrush, clip);
}
}
private void DrawStringIfNeeded(Graphics g)
{
if (VisualMode != ProgressBarDisplayMode.NoText)
{
string text = _textToDraw;
if (!string.IsNullOrEmpty(text))
{
SizeF len = g.MeasureString(text, TextFont);
Point location = new Point((Width / 2) - (int)len.Width / 2, (Height / 2) - (int)len.Height / 2);
g.DrawString(text, TextFont, _textColourBrush, location);
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_textColourBrush?.Dispose();
_progressColourBrush?.Dispose();
}
base.Dispose(disposing);
}
}