Red-Green light indicators in C# .NET Form

Viewed 65696

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.

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; 
        }
8 Answers

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.

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.

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

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

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

I simply used a non enabled button as indicator since I did not manage to install the WinUI for shapes. Same suggestion as question but simplified.

    indicatorButton.Enabled = false;

    ...

    if (allGood)
    {
        indicatorButton.BackColor = Color.Green;
        indicatorButton.Text = "On";
    }
    else
    {
        indicatorButton.BackColor = Color.Red;
        indicatorButton.Text = "Off";
    }
Related