views:

50

answers:

3

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?

+5  A: 

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);
klausbyskov
I have seen example of using color on tutorials... ok.. How to do that with Control?
Toktik
@Toktik I have updated my answer.
klausbyskov
+2  A: 

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

Andrew Barber
+2  A: 

set color to control's ForeColor property. this will set the desired color of your font. You cannot directly set color to font. you will have to set font and forecolor separately for control.

Rajesh Rolen- DotNet Developer