0

I am working on an desktop application in C# that requires a panel background to be filled using dynamic values.For example if the panel of width 200px and the progress property is currently reached at 50% then 100px of the panel should have green color in it rest 100px will be as it.

Nitin Singh
  • 159
  • 2
  • 3
  • 14
  • I did try difference panel_paint event but with no luck – Nitin Singh Sep 25 '19 at 17:26
  • 3
    Surely using a ProgressBar would be easier (Aero knows what progressbars are , so it will animate it suitably etc)? If you're desperate to roll your own wouldn't get into painting myown rectangle; I'd just add another panel inside the first one, docked to the left and width = 200 * 50/100, make the backcolour of the new panel green, and change its width as progress occurs – Caius Jard Sep 25 '19 at 17:29
  • _I did try difference panel_paint event but with no luck_ What are we to make of that? Paint is the right event; show your code!!! You trigger it from whereever the progress is made or noted by calling panel.Invalidate(). You fill a rectangles with e.Graphics.FillRectangle(Brushes.Green) and set the Backcolor to Color.Red. Do DoubleBuffer the Panel!! - Or use Caius' advice if using two panels! – TaW Sep 25 '19 at 17:37
  • @CaiusJard Actually i did tried your idea about using the progress bar but unfortunately using progress bar comes with its own challenges of using different colors in that. Though panel inside panel is i am going to try. – Nitin Singh Sep 25 '19 at 17:48
  • We have to see your paint code in order to help you. – LarsTech Sep 25 '19 at 17:56
  • Create the green rectangle at design time. Make it invisible. When the time is right, make it visible. I would look for a progress bar that can have a gradient color and play with the gradient. – CodingYoshi Sep 25 '19 at 17:57
  • @NitinSingh color your own progress bar (but read all answers-accepted answer might not be what you want): https://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-sharp-net-3-5 - remember that choosing the user's colors *is not your job*; the user has picked the windows theme they want, including the color of the progress bar they want. You might pick a color they cant see well.. I still recommend you just use progressbar and if you want it green, set your Windows to green theme, but leave my Windows' blue progressbars blue ;) – Caius Jard Sep 25 '19 at 18:05

1 Answers1

2

here's a simple control that does the job

public partial class ProgressPanel : Panel
{
    private float m_progress = 0;
    private Color m_progressColor = Color.Green;
    public ProgressPanel()
    {
        InitializeComponent();
    }

    /// <summary>
    /// the progress value is between 0 & 100 inclusively
    /// </summary>
    public float Progress
    {
        get
        {
            return m_progress;
        }
        set
        {
            m_progress = value;
            this.Invalidate();
        }
    }

    public Color ProgressColor
    {
        get
        {
            return m_progressColor;
        }
        set
        {
            m_progressColor = value;
            this.Invalidate();
        }
    }

    private void ProgressPanel_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.Clear(BackColor);

        e.Graphics.FillRectangle(new SolidBrush(ProgressColor), new Rectangle(new Point(), new Size((int)(Width * Progress / 100), Height)));
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // ProgressPanel
        // 
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.ProgressPanel_Paint);
        this.ResumeLayout(false);

    }
}

just create a new empty class in your project & name it ProgressPanel then copy the above code into it.

now you can use your newly created ProgressPanel as you would use any other control from the designer

note that this example is a simplified one. you may notice some flickering, but other than this it's totally functional

if you want to know how to upgrade this example to a professional control, I'd be happy to help

Mostafa Mahmoud
  • 182
  • 1
  • 10