C# 更改特定字符串的标签 ForeColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10421457/
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
change label ForeColor for specific string
提问by artman
I'm writing a WinForms application. I have many labels which represent some status (Enabled/Disabled etc). I want each status to be different color (green, grey...). The only way I know to achive this is to check whether label.Text matches required status and then change its ForeColor. But this is a hard way, as I need to perform this check every time I set status label in my application.
我正在编写一个 WinForms 应用程序。我有很多标签代表某种状态(启用/禁用等)。我希望每个状态都是不同的颜色(绿色、灰色......)。我知道实现这一目标的唯一方法是检查 label.Text 是否与所需状态匹配,然后更改其 ForeColor。但这是一种艰难的方法,因为每次在应用程序中设置状态标签时,我都需要执行此检查。
I wonder if there is any way to do it "in one place?". For example writing a method that will check all labels for specific string and just call it from Form_Load()
我想知道是否有任何方法可以“在一个地方”做到这一点?例如编写一个方法来检查特定字符串的所有标签,然后从 Form_Load() 调用它
采纳答案by Antonio Bakula
Enumerate all labels on Form using Form Controls and it's child controls, for example use GetAll method from this SO answer :
使用表单控件枚举表单上的所有标签及其子控件,例如使用此 SO 答案中的 GetAll 方法:
How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
如何获取特定类型(按钮/文本框)的 Windows 窗体窗体的所有子控件?
And then set appropriate color, something like this :
然后设置适当的颜色,如下所示:
var allLabels = GetAll(form, typeof(Label)); /// you can use this as first param if in form methods
foreach(Label oneLabel in allLabels)
{
if (oneLabel.Text == "Something")
{
// set color or whatever
}
}
回答by Ryan Bennett
Add all your labels to a List on Form_Load(), then you can do a .Where(l => l.Text == "String you are looking for"). Iterate through those to set the forecolor appropriately
将所有标签添加到 Form_Load() 上的列表中,然后您可以执行 .Where(l => l.Text == "String you are looking for")。遍历这些以适当地设置前景色
回答by c0deNinja
You could create your own custom label class which inherits from the Label class. Something like this:
您可以创建自己的自定义标签类,该类继承自 Label 类。像这样的东西:
public class MyLabel : Label
{
public MyLabel()
{
if(this.Text == "something")
{
this.ForeColor = Color.Green;
}
}
protected override void OnTextChanged(EventArgs e)
{
if(this.Text == "something")
{
this.ForeColor = Color.Green;
}
}
}
And so instead of using Label use MyLabel everywhere.
因此,不要在任何地方使用 Label 使用 MyLabel。
回答by Firoz Ansari
You can also take advantage of Tag property of label control. Set Tag to "Status" for all label controls you are using to display status and in Page_Load event, rotate a loop to set ForeColor.
您还可以利用标签控件的 Tag 属性。将用于显示状态的所有标签控件的 Tag 设置为“Status”,并在 Page_Load 事件中旋转循环以设置 ForeColor。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control In Me.Controls
If (TypeOf (control) Is Label) Then
Dim statusLabel As Label
statusLabel = control
If (statusLabel.Tag = "Status") Then
statusLabel.ForeColor = Color.Red
End If
End If
Next
End Sub

