Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:53:43+00:00 2026-06-17T13:53:43+00:00

I have a custom user Progress Bar control and have overridden its Font property

  • 0

I have a custom user Progress Bar control and have overridden its Font property in the process of displaying text.

When I drop a copy of my usercontrol onto a form, I can set the Font property just fine, but I don’t see the value I set for ‘Font’ showing up in my form’s designer file. When I compile/run my app, the value I entered is lost.

Here is the code as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ProgressBarWithText
{
    /// <summary>
    /// Control that extends the System.Windows.Forms.ProgressBar with
    /// the ability to overlay the percentage or a text message.
    /// </summary>
    [Description(
        "Control that extends the System.Windows.Forms.ProgressBar with the ability to overlay the Text."),
    DefaultProperty("TextVisible"),
    DefaultEvent("TextChanged")]
    public class ProgressBarWithText : ProgressBar
    {
        private const int WM_PAINT = 0x0F;

        /// <summary>
        /// Raised when the visibility of the percentage text is changed.
        /// </summary>
        [Description("Raised when the visibility of the percentage text is changed."),
        Category("Property Changed")]
        public event EventHandler TextVisibleChanged;

        /// <summary>
        /// Raised when the text has changed.
        /// </summary>
        [Description("Raised when the text has changed."),
        Category("Property Changed")]
        public event EventHandler TextChanged;

        /// <summary>
        /// Raised when the font has changed.
        /// </summary>
        [Description("Raised when the font has changed."),
        Category("Property Changed")]
        public event EventHandler FontChanged;

        //private ContentAlignment m_p_align;
        private Font m_Font;
        private Color m_overlayColor;
        private StringFormat m_stringFormat;
        private string m_Text;
        private bool m_TextVisible;

        /// <summary>
        /// Create a new instance of a ProgressbarWithPercentage.
        /// </summary>
        public ProgressBarWithText()
        {
            InitializeComponent();

            m_overlayColor = Color.White;
            m_stringFormat = new StringFormat();
            m_TextVisible = true;
            m_stringFormat.Alignment = StringAlignment.Center;
            m_stringFormat.LineAlignment = StringAlignment.Center;
            if (m_Font == null)
                m_Font = SystemFonts.DialogFont;
        }

        #region Properties

        /// <summary>
        /// Get or Sets the Font of the Text being displayed.
        /// </summary>
        [Bindable(true),
        Browsable(true),
        Category("Appearance"),
        Description("Get or Sets the Font of the Text being displayed."),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override Font Font
        {
            get
            {
                return m_Font;
            }
            set
            {
                if (m_Font != value)
                {
                    m_Font = value;
                    Invalidate();
                    OnFontChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets or sets the Color that is used to draw the text over a filled section of the progress bar.
        /// </summary>
        [Browsable(true),
        Description("The Color that is used to draw the text over a filled section of the progress bar."),
        Category("Appearance"),
        DefaultValue(typeof(Color), "White")]
        public Color OverlayColor
        {
            get { return m_overlayColor; }
            set
            {
                if (m_overlayColor != value)
                    m_overlayColor = value;
            }
        }

        /// <summary>
        /// Gets or Sets the Text being displayed.
        /// </summary>
        [Browsable(true),
        Category("Appearance"),
        Description("The Text to be displayed or if left null will display a percentage"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override string Text
        {
            get
            {
                if (m_Text != "") return m_Text;
                return Value.ToString() + "%";
            }

            set
            {
                if (m_Text != value)
                {
                    m_Text = value;
                    Invalidate();
                    OnTextChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets or sets a value that indicates whether the percentage will be displayed.
        /// </summary>
        [Browsable(true),
        Description("Indicates whether the percentage will be displayed on the progress bar."),
        Category("Appearance"),
        DefaultValue(true)]
        public bool TextVisible
        {
            get { return m_TextVisible; }
            set
            {
                if (m_TextVisible != value)
                {
                    m_TextVisible = value;
                    OnTextVisibleChanged(EventArgs.Empty);
                }
            }
        }

        public new int Value
        {
            get { return base.Value; }
            set
            {
                if (base.Value != value)
                {
                    base.Value = value;

                    /* Needed for XP. Downside is control will be drawn twice
                     * when value coincides with one that the system uses for
                     * repaint. Could maybe use Environment.OSVersion to check? */
                    if (m_TextVisible)
                        Invalidate();
                }
            }
        }

        #endregion Properties

        #region Event Handlers

        protected virtual void OnTextVisibleChanged(EventArgs e)
        {
            EventHandler eh = TextVisibleChanged;
            if (eh != null)
                eh(this, e);
        }

        protected virtual void OnTextChanged(EventArgs e)
        {
            EventHandler eh = TextChanged;
            if (eh != null)
                eh(this, e);
        }

        protected virtual void OnFontChanged(EventArgs e)
        {
            EventHandler eh = FontChanged;
            if (eh != null)
                eh(this, e);
        }

        #endregion Event Handlers

        private void ShowText()
        {
            using (Graphics graphics = CreateGraphics())
            {
                // Draw left side
                Region regionLeft = new Region(new RectangleF(
                    ClientRectangle.X,
                    ClientRectangle.Y,
                    (ClientRectangle.Width * base.Value) / 100,
                    ClientRectangle.Height));
                using (Brush brush = new SolidBrush(m_overlayColor))
                {
                    graphics.Clip = regionLeft;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
                // Draw right side
                Region regionRight = new Region(ClientRectangle);
                regionRight.Exclude(regionLeft);
                using (Brush brush = new SolidBrush(ForeColor))
                {
                    graphics.Clip = regionRight;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m_TextVisible && m.Msg == WM_PAINT)
                ShowText();
        }
    }
}

The following is what is saved in the design file after the ‘font’ property has been changed:

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.progressBarWithText1 = new ProgressBarWithText.ProgressBarWithText();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(13, 104);
            this.trackBar1.Maximum = 100;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(596, 45);
            this.trackBar1.TabIndex = 1;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
            // 
            // progressBarWithText1
            // 
            this.progressBarWithText1.Location = new System.Drawing.Point(13, 13);
            this.progressBarWithText1.Name = "progressBarWithText1";
            this.progressBarWithText1.Size = new System.Drawing.Size(596, 85);
            this.progressBarWithText1.TabIndex = 2;
            this.progressBarWithText1.Text = "foo";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(621, 262);
            this.Controls.Add(this.progressBarWithText1);
            this.Controls.Add(this.trackBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TrackBar trackBar1;
        private ProgressBarWithText.ProgressBarWithText progressBarWithText1;
    }
}

As you can see there is no parameters set for progressBarWithText1 for the font property. Is there a way to force them to be saved without having to manually set them or is there something I am doing wrong and need further instruction on?

Thank you for your time into this. God Bless,

Craig

Ps. This control is not entirely finished. I am just writing it now and got hung up on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:53:44+00:00Added an answer on June 17, 2026 at 1:53 pm

    Here is the Answer to the question:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace ProgressBarWithText
    {
        /// <summary>
        /// Control that extends the System.Windows.Forms.ProgressBar with
        /// the ability to overlay the percentage or a text message.
        /// </summary>
        [Description(
            "Control that extends the System.Windows.Forms.ProgressBar with the ability to overlay the Text."),
        DefaultProperty("TextVisible"),
        DefaultEvent("TextChanged")]
    
        public class ProgressBarWithText : ProgressBar
        {
            private const int WM_PAINT = 0x0F;
    
            /// <summary>
            /// Raised when the visibility of the percentage text is changed.
            /// </summary>
            [Description("Raised when the visibility of the percentage text is changed."),
            Category("Property Changed")]
            public event EventHandler TextVisibleChanged;
    
            /// <summary>
            /// Raised when the text has changed.
            /// </summary>
            [Browsable(true),
            Description("Raised when the text has changed."),
            Category("Property Changed")]
            public new event EventHandler TextChanged;
    
            /// <summary>
            /// Raised when the font has changed.
            /// </summary>
            [Browsable(true),
            Description("Raised when the font has changed."),
            Category("Property Changed")]
            public new event EventHandler FontChanged;
    
            //private ContentAlignment m_p_align;
            private Color m_overlayColor;
            private StringFormat m_stringFormat;
            private string m_Text;
            private bool m_TextVisible;
    
            /// <summary>
            /// Create a new instance of a ProgressbarWithPercentage.
            /// </summary>
            public ProgressBarWithText()
            {
                m_overlayColor = Color.White;
                m_stringFormat = new StringFormat();
                m_TextVisible = true;
                m_stringFormat.Alignment = StringAlignment.Center;
                m_stringFormat.LineAlignment = StringAlignment.Center;
            }
    
            #region Properties
    
            /// <summary>
            /// Get or Sets the Font of the Text being displayed.
            /// </summary>
            [Bindable(true),
            Browsable(true),
            Category("Appearance"),
            Description("Get or Sets the Font of the Text being displayed."),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
            EditorBrowsable(EditorBrowsableState.Always)]
            public override Font  Font
            {
                get 
                { 
                    return base.Font;
                }
                set 
                { 
                    base.Font = value;
                    Invalidate();
                    OnFontChanged(EventArgs.Empty);
                }
            }
    
            /// <summary>
            /// Gets or sets the Color that is used to draw the text over a filled section of the progress bar.
            /// </summary>
            [Browsable(true),
            Description("The Color that is used to draw the text over a filled section of the progress bar."),
            Category("Appearance"),
            DefaultValue(typeof(Color), "White")]
            public Color OverlayColor
            {
                get { return m_overlayColor; }
                set
                {
                    if (m_overlayColor != value)
                        m_overlayColor = value;
                }
            }
    
            /// <summary>
            /// Gets or Sets the Text being displayed.
            /// </summary>
            [Browsable(true),
            Category("Appearance"),
            Description("The Text to be displayed or if left null will display a percentage"),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
            EditorBrowsable(EditorBrowsableState.Always)]
            public override string Text
            {
                get
                {
                    if (m_Text != "") return m_Text;
                    return Value.ToString() + "%";
                }
    
                set
                {
                    if (m_Text != value)
                    {
                        m_Text = value;
                        Invalidate();
                        OnTextChanged(EventArgs.Empty);
                    }
                }
            }
    
            /// <summary>
            /// Gets or sets a value that indicates whether the percentage will be displayed.
            /// </summary>
            [Browsable(true),
            Description("Indicates whether the Text will be displayed on the progress bar."),
            Category("Appearance"),
            DefaultValue(true)]
            public bool TextVisible
            {
                get { return m_TextVisible; }
                set
                {
                    if (m_TextVisible != value)
                    {
                        m_TextVisible = value;
                        OnTextVisibleChanged(EventArgs.Empty);
                    }
                }
            }
    
            public new int Value
            {
                get { return base.Value; }
                set
                {
                    if (base.Value != value)
                    {
                        base.Value = value;
    
                        /* Needed for XP. Downside is control will be drawn twice
                            * when value coincides with one that the system uses for
                            * repaint. Could maybe use Environment.OSVersion to check? */
                        if (m_TextVisible)
                            Invalidate();
                    }
                }
            }
    
            #endregion Properties
    
            #region Event Handlers
    
            protected virtual void OnTextVisibleChanged(EventArgs e)
            {
                EventHandler eh = TextVisibleChanged;
                if (eh != null)
                    eh(this, e);
            }
    
            protected override void OnTextChanged(EventArgs e)
            {
                EventHandler eh = TextChanged;
                if (eh != null)
                    eh(this, e);
            }
    
            protected override void OnFontChanged(EventArgs e)
            {
                EventHandler eh = FontChanged;
                if (eh != null)
                    eh(this, e);
            }
    
            #endregion Event Handlers
    
            private void ShowText()
            {
                using (Graphics graphics = CreateGraphics())
                {
                    // Draw left side
                    Region regionLeft = new Region(new RectangleF(
                        ClientRectangle.X,
                        ClientRectangle.Y,
                        (ClientRectangle.Width * base.Value) / 100,
                        ClientRectangle.Height));
                    using (Brush brush = new SolidBrush(m_overlayColor))
                    {
                        graphics.Clip = regionLeft;
                        graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                    }
                    // Draw right side
                    Region regionRight = new Region(ClientRectangle);
                    regionRight.Exclude(regionLeft);
                    using (Brush brush = new SolidBrush(ForeColor))
                    {
                        graphics.Clip = regionRight;
                        graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                    }
                }
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m_TextVisible && m.Msg == WM_PAINT)
                    ShowText();
            }
        }
    }
    

    And if you want to learn more about the Virtual, New, and Override, you can find them on the Understanding C# web-site.

    Thanks for your time.

    God Bless,

    Craig

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom user control with a progress overlay to get a
I have a custom user control extending the Listbox class. Inside of it I
I have a custom user control on my windows forms. This control has a
I have a custom user control called ErrorNotificationBox. To place that on my page
I have a custom base user control in silverlight. <UserControl x:Class=Problemo.MyBaseControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008
I have 2 views (first view - custom user control, MainWindow view - main
Background: I have a custom user control based around a WPF TreeView. I've been
I have created Custom User Control which contain TextBox and PasswordBox. it is binding
I have a custom user control that I am adding to a page via
I am writing a custom user control, called MyUserControl. I have many DependecyProperties for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.