在 C# 中更改标签的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15906090/
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 color of Label in C#
提问by Gee_Djo
I'm working in a chat program using C# and i need to give to every user a different color , =>So I need a function to change color of writing in C#
我正在使用 C# 的聊天程序中工作,我需要为每个用户提供不同的颜色,=>所以我需要一个函数来更改 C# 中的书写颜色
Thanks
谢谢
回答by musefan
I am going to assume this is a WinForms questions (which it feels like, based on it being a "program" rather than a website/app). In which case you can simple do the following to change the text colour of a label:
我将假设这是一个 WinForms 问题(感觉就像,基于它是一个“程序”而不是网站/应用程序)。在这种情况下,您可以简单地执行以下操作来更改标签的文本颜色:
myLabel.ForeColor = System.Drawing.Color.Red;
Or any other colour of your choice. If you want to be more specific you can use an RGB value like so:
或您选择的任何其他颜色。如果你想更具体,你可以使用像这样的 RGB 值:
myLabel.ForeColor = Color.FromArgb(0, 0, 0);//(R, G, B) (0, 0, 0 = black)
Having different colours for different users can be done a number of ways. For example, you could allow each user to specify their own RGB value colours, store these somewhere and then load them when the user "connects".
可以通过多种方式为不同的用户使用不同的颜色。例如,您可以允许每个用户指定他们自己的 RGB 值颜色,将它们存储在某处,然后在用户“连接”时加载它们。
An alternative method could be to just use 2 colours - 1 for the current user (running the app) and another colour for everyone else. This would help the user quickly identify their own messages above others.
另一种方法可能是只使用 2 种颜色 - 当前用户(运行应用程序)使用 1 种颜色,其他人使用另一种颜色。这将帮助用户快速识别他们自己的消息而不是其他消息。
A third approach could be to generate the colour randomly - however you will likely get conflicting values that do not show well against your background, so I would suggest not taking this approach. You could have a pre-defined list of "acceptable" colours and just pop one from that list for each user that joins.
第三种方法可能是随机生成颜色 - 但是您可能会得到在您的背景下无法很好显示的相互冲突的值,因此我建议不要采用这种方法。您可以有一个“可接受”颜色的预定义列表,然后为每个加入的用户从该列表中弹出一个。
回答by Arshad
You can try this with Color.FromArgb
:
你可以试试这个Color.FromArgb
:
Random rnd = new Random();
lbl.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));