C# .NET 形式的红绿灯指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1923334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:53:02  来源:igfitidea点击:

Red-Green light indicators in C# .NET Form

c#winforms

提问by NealWalters

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

在 C# 表单上显示红/绿灯指示器的最快方法是什么?

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.

基本上,我正在编写一个特定于应用程序的小监视器,如果某些服务关闭或某些 Web 服务没有响应,它会显示红灯。

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

采纳答案by Andrew Flanagan

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.

有一些优秀的图标和图形这样的事情......这里有一个链接在这里。更多免费或小额费用提供。

回答by Neil N

I would just make a panel or PictureBoxand 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.

我只是制作一个面板或PictureBox将背景图像设置为红/绿灯的图像。要么在 PhotoShop/PaintShop/MS Paint 中制作图像,要么从网上下载一些库存图像。

Whenever the status changes, just swap the image out.

每当状态发生变化时,只需将图像换出即可。

回答by John Kraft

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

我只是使用一些标准图像并将它们放在picturebox. 在我们的应用程序上效果很好。

回答by Matt Davis

Create red and green bitmaps and use the PictureBoxcontrol to show the bitmaps.

创建红色和绿色位图并使用PictureBox控件显示位图。

回答by Gabe

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

使用图像,但这里有一些很棒的图标可用因此您不必实际制作一些。

回答by Jesse C. Slicer

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.

但这一切都取决于您(或您的组织)对可读性和可维护性的定义。

回答by Chef Pharaoh

This is simple, just use System.Windows.Shapesfor the object and System.Windows.Media.Brushesfor the colors.

这很简单,只System.Windows.Shapes用于对象和System.Windows.Media.Brushes颜色。

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"));

回答by Alpha

just try this it works for me.

试试这个它对我有用。

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