-1

How can you customize the color of a ProgressBar in a Windows Forms application using C#? Explain the steps or methods you would use to change the color of the ProgressBar based on specific conditions or to create a visually appealing user interface.

progressBar.ForeColor = Color.Blue;

Custom ProgressBar changes color to indicate password strength in C# Windows Forms app.

deadwards
  • 2,109
  • 1
  • 16
  • 27
  • [Prevent flickering of custom ProgressBar](https://stackoverflow.com/a/66521254/7444103) - Customize as you prefer -- Or [this other](https://stackoverflow.com/a/62053257/7444103) – Jimi Aug 14 '23 at 09:39
  • Consider just creating a custom control that is [user-drawn](https://learn.microsoft.com/en-in/dotnet/desktop/winforms/controls/user-drawn-controls?view=netframeworkdesktop-4.8). That way you can customize the appearance however you like. And it might be less confusing than using a progress bar for non-progress related tasks. – JonasH Aug 14 '23 at 11:23

1 Answers1

0

You could implement your own progress bar to meet your needs. Here is an example. Find the comments below.

using System.Drawing;
using System.Windows.Forms;

namespace Test
{
    /// <summary>
    /// Enumeration of password types.
    /// </summary>
    public enum PasswordStrength
    {
        None = 0,
        Weak = 1,
        Moderate = 2,
        Strong = 3
    }

    /// <summary>
    /// Implements a custom control that visualizes the password strengthness.
    /// </summary>
    public class PasswordStrengthControl : Control
    {
        private PasswordStrength _strength;
        protected Color[] Colors { get; set; } 

        /// <summary>
        /// Gets or sets the password strengthness.
        /// </summary>
        public PasswordStrength Strength
        {
            get { return _strength; }
            set
            {
                _strength = value;
                Invalidate();
            }
        }

        public PasswordStrengthControl()
        {
            Colors = new[] { Color.Red, Color.Orange, Color.Green };
        }

        /// <summary>
        /// Visualize the strengthness.
        /// </summary>
        /// <param name="args">Event arguments.</param>
        protected override void OnPaintBackground(PaintEventArgs args)
        {
            // Fill the background normally.
            base.OnPaintBackground(args);

            // Spacing between the bars.
            var spacing = 5;

            // Total width of the drawing surface.
            var w = Width - Padding.Left - Padding.Right - (spacing * Colors.Length - 1);

            // Total height of the drawing surface.
            var h = Height - Padding.Top - Padding.Bottom;

            // Bar width.
            var width = w / Colors.Length;

            // Color of the bars depending of the password strengthness.
            var color = Strength > PasswordStrength.None ? Colors[(int)Strength - 1] : Color.Transparent;

            using (var borderBrush = new SolidBrush(SystemColors.ActiveBorder))
            using (var borderPen = new Pen(borderBrush, 1))
            using (var fillBrush = new SolidBrush(color))
            {
                // Go through all bars.
                for (var i = 0; i < Colors.Length; i++)
                {
                    // Find the position of each bar.
                    var x = i * width + Padding.Left;
                    if (i > 0)
                    {
                        x += (spacing * i);
                    }

                    // Draw the borders around each bar.
                    args.Graphics.DrawRectangle(borderPen, x, Padding.Top, width - 1, h - 1);

                    if (i < (int)Strength)
                    {
                        // Fill the bars with the respective color depending on the password strengthness.
                        args.Graphics.FillRectangle(fillBrush, new Rectangle(x + 1, Padding.Top + 1, width - 2, h - 2));
                    }
                }
            }
        }
    }
}

This control gives the following output:

enter image description here

Sergey
  • 581
  • 1
  • 5
  • 12