Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# apply Color to Font

I have code like this.

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#101B83");
System.Drawing.Font nameFont = new System.Drawing.Font("Tahoma", 10);
System.Drawing.Font birthdayFont = new System.Drawing.Font("Tahoma", 6);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
nameFont.Color = col;

Last line doesn't work, because .Color field cannot be found. Why?

like image 498
Tigran Tokmajyan Avatar asked Oct 19 '10 08:10

Tigran Tokmajyan


2 Answers

Because a font does not have a color. A control can render text using a font and a color, but the color is not a property of the font.

EDIT:

If you want a textbox that uses a given font and color you can do the following (I'm assuming that you are using winforms):

var myTextBox = new TextBox();
myTextBox.ForeColor = col;
myTextBox.Font = birthdayFont;
myTextBox.Text = "Happy birthday!";

this.Controls.Add(myTextBox);
like image 51
Klaus Byskov Pedersen Avatar answered Oct 23 '22 10:10

Klaus Byskov Pedersen


Fonts do not have colors. You use colors in the drawing code itself, or with the Control.ForeColor property

like image 21
Andrew Barber Avatar answered Oct 23 '22 10:10

Andrew Barber