开发者

C# apply Color to Font

开发者 https://www.devze.com 2023-01-20 08:07 出处:网络
I have code like this. System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(\"#101B83\");

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.Drawin开发者_如何转开发g.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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消