tags:

views:

400

answers:

6

What's the quickest way to show a red/green light indicator on a C# form?

I originally thought about using radio buttons, but not sure how to set the color of the dot, only the foreground/background text.

Then I thought about drawing a circle. Couldn't find a toolbox shape for that, and didn't want to write code just to draw the circle.

Basically, I'm writing a little application specific monitor, that shows a red light if certain services are down, or certain web services are not responding.

Thanks,

Neal Walters

This is what I have so far using a square button instead of a circle. The code is just what I want, I just want a round shape.

        if (allGood)
        {
            btnIISIndicator.BackColor = Color.Green; 
        }
        else
        {
            btnIISIndicator.BackColor = Color.Red; 
        }
+2  A: 

I would just make a panel or PictureBox and set the Background image to that of a red/green light. Either make the images in PhotoShop/PaintShop/MS Paint or download some stock images off the web.

Whenever the status changes, just swap the image out.

Neil N
+1  A: 

I just use some standard images and put them in a picturebox. works great on our apps.

John Kraft
+1  A: 

Create red and green bitmaps and use the PictureBox control to show the bitmaps.

Matt Davis
+6  A: 

There are some excellent icons and graphics for this sort of thing... Here's a link here. Plenty more available for free or for some small charge.

Andrew Flanagan
+1 for providing an actual link to images.
alastairs
As I'm a coder, not an artist drawer, this seems best. I thought there used to be a "shape" or "circle" on the toolbox "in the old days"?
NealWalters
FYI - not as simpel as I had hoped - I may have to post another question for this: {"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resource1.resources\" was correctly embedded or linked into assembly \"TFBIC.RCT.Monitor\" at compile time, or that all the satellite assemblies required are loadable and fully signed."}
NealWalters
For the "Build Action" on a resource you'll want it to be "Embedded Resource" in order to use it (so far as I can tell from your code). Let me know if that works.
Andrew Flanagan
It took me over an hour to get the Resource Manager working without error. That was due to my lack of knowledge. Now I have the a green button with an unwanted white background on my default gray form. Is there a transparency option? I saved the images from the PowerPoint to disk as .jpg's. Meanwhile, I went with a regular toolbox button (it's just square instead of round). That was the 2 minutes vs 2 hour solution.
NealWalters
Transparency issues can be a pain -- your best bet is using an image like a PNG that has an actual transparent edge. With a tool like Paint.NET (free) you could probably accomplish that pretty painlessly. There may be a way to set an alpha key (and have it use the white edge of that graphic as the transparent mask) but I'm not aware of how to do it. Having some tools for graphics (resizing, transparency issues, formats) is often kind of necessary i this line of work.
Andrew Flanagan
+1  A: 

Use an image, but theres some great icons available here so you dont have to actually make some.

gmcalab
+1  A: 

Not exactly related to the question at hand, but your code could be shortened somewhat using the ternary operator as such:

btnIISIndicator.BackColor = allGood ? Color.Green : Color.Red;

But that all depends on your (or your organization's) definition of readability and maintainability.

Jesse C. Slicer
Good point, I know ternary operator, but typically forget to code it.
NealWalters