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?

1

3 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); 
1

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy