Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change string color

Tags:

c#

winforms

I have label that contain of several strings and one of those string i want to change it color.

This is what i have try:

private string state = string.Empty;
state = System.Drawing.Color.Blue.ToString();

But it still remained to same color

like image 765
user2214609 Avatar asked Jun 11 '13 20:06

user2214609


3 Answers

As far as I'm aware, a Windows Forms Label can only use a single color for the whole of its text. If you want multi-colored text, you'll either need to use multiple labels or use RichTextBox... or perform the painting yourself, of course.

like image 114
Jon Skeet Avatar answered Nov 04 '22 09:11

Jon Skeet


You have to change the colour of the label, not the string.

So you'd have a label on your form, say LabelTest, then in your code would look like this:

string state = "Some text for our label";
LabelTest.Text = state;
LabelTest.ForeColor = System.Drawing.Color.Blue;

As has been mentioned in other answers, to use multiple colours, you'd need multiple labels, each with their text and colour set separately.

like image 38
xen-0 Avatar answered Nov 04 '22 10:11

xen-0


Label cannot contain items of more than one color. Use more labels or some other kind of control. But from the code you pasted I recommend to go through some .NET tutorial. You probably miss the basic concepts.

like image 34
rocky Avatar answered Nov 04 '22 08:11

rocky