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
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.
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.
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.
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.
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.
You can try this with Color.FromArgb
:
Random rnd = new Random();
lbl.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With