Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Font Style

Tags:

c#

I have a label with text in Bold and Italic . I want to change those font properties through a button click.

I got to know of the code Label1.Font = new Font(Label1.Font, FontStyle.Regular);

But from this code it will undo both BOLD & ITALIC properties. I want only to remove bold property.....

Are there anything like fontsyle.bold = false ?

like image 946
udarabibile Avatar asked Jul 20 '13 07:07

udarabibile


3 Answers

Use Font.Style of original font when creating new one, use & ~ to flip styles

   label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Bold);
like image 88
Alexei Levenkov Avatar answered Oct 06 '22 16:10

Alexei Levenkov


You can try this also --

label1.Font = new Font("Arial", 24,FontStyle.Bold);

or

mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);

The constructor takes different parameters. see more

like image 44
Ishan Jain Avatar answered Oct 06 '22 18:10

Ishan Jain


The best option is to use bitcodes and the XOR operator ^

try this code:

Label1.Font = new Font(Label1.Font.Style ^ FontStyle.Regular);
like image 44
Diego Avatar answered Oct 06 '22 16:10

Diego