Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of progressbar in C# .NET 3.5?

I'd like to do two things on my progress bar.

  1. Change the green colour to red.
  2. Remove the blocks and make it in one color.

Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!

Thanks.

like image 212
Ivan Prodanov Avatar asked Apr 22 '09 19:04

Ivan Prodanov


People also ask

How do I set progressbar value?

You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100.

How do I set Progressbar text?

Alliteratively you can try placing a Label control and placing it on top of the progress bar control. Then you can set whatever the text you want to the label.


1 Answers

OK, it took me a while to read all the answers and links. Here's what I got out of them:

Sample Results

The accepted answer disables visual styles, it does allow you to set the color to anything you want, but the result looks plain:

enter image description here

Using the following method, you can get something like this instead:

enter image description here

How To

First, include this if you haven't: using System.Runtime.InteropServices;

Second, you can either create this new class, or put its code into an existing static non-generic class:

public static class ModifyProgressBarColor {     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);     public static void SetState(this ProgressBar pBar, int state)     {         SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);     } } 

Now, to use it, simply call:

progressBar1.SetState(2);

Note the second parameter in SetState, 1 = normal (green); 2 = error (red); 3 = warning (yellow).

Hope it helps!

like image 187
user1032613 Avatar answered Oct 16 '22 01:10

user1032613