开发者

Red-Green light indicators in C# .NET Form

开发者 https://www.devze.com 2022-12-14 19:19 出处:网络
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

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 monit开发者_开发问答or, 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; 
        }


This is simple, just use System.Windows.Shapes for the object and System.Windows.Media.Brushes for the colors.

For a circle you can do the following:

System.Windows.Shapes.Ellipse circle = new System.Windows.Shapes.Ellipse();
circle.Height = 20; //or some size
circle.Width = 20; //height and width is the same for a circle
circle.Fill = System.Windows.Media.Brushes.Red;

Then you can make a function to do your check for red and green.

Also, you can use hex values for the colors as well:

circle.Fill = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#RRGGBB"));


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 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.


just try this it works for me.

SolidColorBrush solidColor=new SolidColorBrush();
  solidColor.Color=Colors.Red;
 ellips_circle.Fill=solidColor;


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


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


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


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";
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消