Jul 05
Changing a font style is a bit easier than changing its size, as there is a Font constructor that accepts a font and style as arguments. For example, to bold a label’s font:
Label label = new Label(); . . . label.Font = new Font( label.Font, FontStyle.Bold );
If you want to keep the original style but also bold it:
label.Font = new Font( label.Font, label.Font.Style | FontStyle.Bold );
The problem with the approach above is that a new Font object is created whether it’s needed or not. Here is a handy method that will bold a font, creating a new Font object only if necessary:
static public Font BoldFont( Font font ) { if (font != null) { FontStyle fontStyle = font.Style; if ((fontStyle & FontStyle.Bold) == 0) { fontStyle |= FontStyle.Bold; font = new Font( font, fontStyle ); } } return font; }
For example, to bold a label’s font:
label.Font = BoldFont( label.Font );
You code is correct in only case that all text is the same font name. I mean if your text mixes font(fontname),you coldn’t do like that. I have trouble to change fontstyle with the text that has one part in Times new roman and one part in Arial
THANK YOU!
A, BTW:
If you want to clear all styles fast use this method:
static public Font CleanFont(Font font)
{ if (font != null)
{ FontStyle fontStyle = font.Style;
fontStyle &= FontStyle.Bold;
fontStyle &= FontStyle.Italic;
fontStyle &= FontStyle.Strikeout;
fontStyle &= FontStyle.Underline;
font = new Font(font, fontStyle);
}
return font;
}
If my FontStyle is set to Bold AND Italic, then I set fontStyle &= FontStyle.Italic;
in order to keep activated just the Bold style, the system switch off all the styles so as a result the font is turned into Regular.
How can I switch off just a style (Bold, Italic or Underline) at a time?
Thanks!
C.
To clear a flag in an enumeration, you would use the following format:
fontStyle &= ~FontStyle.Italic;
Note the tilde ~ before FontStyle.Italic. This inverts the flag, then you are AND-ing it with the original fontStyle. This preserves all flags except FontStyle.Italic, which effectively “shuts off” the italic style.
Very helpful… Many thanks!
Lable mylable=new Lable();
mylable.Font.Bold=true;
Sanjay, that doesn’t work. Font.Bold is a read-only property (no setter).
You can set it to FontStyle.Regular to clean all styles fast.
Use the string value to create a font family object then create and assign a new font with that font family object and the size as a float
FontFamily fm = new FontFamily(“Mistral”);
lblTime.Font = new Font(fm, 45);
thank thats what i need
i have to convert change the font and fontstyle of a lable control to “ML-TTKarthika” and “bold” respectively, i could change font by writing this code
FontFamily fm = new FontFamily(“ML-TTKarthika”);
label2.Font = new Font(fm, 12);
but i couldn’t cahnge fontstyle. when i written this code label1.Font.font = true; change the fontstyle a error message shows that
Error 1 Property or indexer ‘System.Drawing.Font.Bold’ cannot be assigned to — it is read only
To change a font you do need to create a new font using the constructor.
Unfortunately you can’t adjust it after creation.
Font myNewFont = new Font(myOldFont.Name, myOldFont.Size, myOldFont.Style);
will generate the same font again.
Add any changes you need, but to the parameters of the constructor call.
float myNewSize = myOldFont.Size * 1.25;
Font myNewFont = new Font(myOldFont.Name, myNewSize, myOldFont.Style);