12

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#

Thanks

3
  • WinForms, WPF, ASP.NET?
    – p.s.w.g
    Apr 9, 2013 at 15:29
  • you could add 5 in rgb values, like: Color.FromArgb(5, 5, 5); And next time: Color.FromArgb(10, 10, 10); orso
    – Max
    Apr 9, 2013 at 15:30
  • 1
    Just a comment to the fact that the question is "closed as not constructive". I read this in another question around and agree completely: most of the times, this simple questions are more helpful than documentation per se (especially MSDN...). So please, close it but do not remove it. Apr 15, 2014 at 6:49

2 Answers 2

40

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:

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:

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

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.

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.

9

You can try this with Color.FromArgb:

Random rnd = new Random();
lbl.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));

Not the answer you're looking for? Browse other questions tagged or ask your own question.