Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of Label in C# [closed]

Tags:

c#

.net

chat

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

like image 719
Gee_Djo Avatar asked Apr 09 '13 15:04

Gee_Djo


People also ask

How do you change the color of a label?

Scroll down to your labels, and tap the label you want to change the color of. three dots icon in the top-right corner. Select Edit label. Tap on the current color name and select your color of choice.

How do you color a label in C#?

Step 1: Create a label using the Label() constructor is provided by the Label class. // Creating label using Label class Label mylab = new Label(); Step 2: After creating Label, set the ForeColor property of the Label provided by the Label class. // Set ForeColor property of the label mylab.

What is ForeColor C#?

The ForeColor property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default.

How do I change the color of a label in VB net?

No need to set dim you can just use forecolor property. now when you click on button_1 and the label. 1 text color get changed.


2 Answers

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.

like image 68
musefan Avatar answered Oct 12 '22 22:10

musefan


You can try this with Color.FromArgb:

Random rnd = new Random();
lbl.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
like image 33
Arshad Avatar answered Oct 12 '22 23:10

Arshad