Jul 05
An inspection of the Font class will reveal that every public property is read-only. This means to change a font’s size, you need to create a new Font object with all the same properties of your current font but with the new size. Here is a handy method to do just that:
static public Font ChangeFontSize( Font font, float fontSize ) { if (font != null) { float currentSize = font.Size; if (currentSize != fontSize) { font = new Font( font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont ); } } return font; }
For example, to double the size of a label’s font:
label.Font = ChangeFontSize( label.Font, label.Font.Size * 2 );
Graphics Unit
Note the method above uses the same GraphicsUnit (point, pixel, millimeter, etc.) as the original font. You may want to overload this method to also accept a specific unit:
static public Font ChangeFontSize( Font font, float fontSize, GraphicsUnit unit ) { if (font != null) { float currentSize = font.Size; if (currentSize != fontSize) { font = new Font( font.Name, fontSize, font.Style, unit, font.GdiCharSet, font.GdiVerticalFont ); } } return font; }
For example, to resize a label’s font to 12 pixels:
label.Font = ChangeFontSize( label.Font, 12.0F, GraphicsUnit.Pixel );
hi
i tried to use the vb version of your code :
i tried : Lbl.Font = ChangeFontSize(“Tahoma”, 8) without success !!! Why ? is there a way to use this kind of font name (“Tahoma”) ?
thanks
Public Shared Function ChangeFontSize(ByVal font As Font, ByVal fontSize As Single) As Font
If font IsNot Nothing Then
Dim currentSize As Single = font.Size
If currentSize fontSize Then
font = New Font(font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont)
End If
End If
Return font
End Function
Re: i tried : Lbl.Font = ChangeFontSize(“Tahoma”, without success !!! Why ? is there a way to use this kind of font name (“Tahoma”) ?
The first argument of the ChangeFontSize function is a Font object, and you are attempting to pass it a string (“Tahoma”).
The ChangeFontSize function was provided to change the size of an existing font. What you are trying to do is change to another font altogether (Tahoma in your example). In that case, you can create a new Font object:
Lbl.Font = New Font(“Tahoma”, 12.0F)
thank`s you hellp me in my project by this code
Ok, already it works.
It’s a nice trick! Thanks.
gets this below error in line (lbobjective.font parameter)
lbObjective.Font = ChangeFontSize(lbObjective.Font , fontSizeta2);
Error cannot convert from ‘System.Web.UI.WebControls.FontInfo’ to ‘System.Drawing.Font’
Fonts = ((DropDownList)Master.FindControl(“ddlLanguage”)).SelectedValue;
if (Fonts == “ta-IN”)
{
Double fontSizeta = Convert.ToDouble(lbObjective.Font.Size);
fontSizeta = fontSizeta * 0.8;
float fontSizeta2 = (float)fontSizeta;
lbObjective.Font = ChangeFontSize(lbObjective.Font , fontSizeta2);
}
static public Font ChangeFontSize(Font font, float fontSize)
{
if (font != null)
{
float currentSize = font.Size;
if (currentSize != fontSize)
{
font = new Font(font.Name, fontSize,
font.Style, font.Unit,
font.GdiCharSet, font.GdiVerticalFont);
}
}
return font;
}
help me in solving this ir would be grateful
Thanks in advance
What is lbObjective? From the info you provided, it appears that lbObjective.Font is a FontInfo and not a Font.
Hey i tried this…..its really work great……..
actually i saw many sites…they didnt give any clear solution…thanks a lot…now im very happy 🙂
In my project when i radio button named bold is clicked the word we typed in the text box should change bold..Please tell me the coding..I tried many times but i dint get correct coding… how to change the font style of a word in a text box?
textBox.Font = new Font( textBox.Font, FontStyle.Bold );
How to convert System.Media.Font(WPF) to System.Drawing.Font(C#.Net), including font size ?
thanks alot this help me alot thank you very very very much!!!
thanku
it is soo helpful for my project
foreach (Control ctl in this.Controls)
{
if ((ctl is TextBox)) //|| (ctl is ComboBox) || (ctl is ListBox) || (ctl is ListView))
{
ctl.Font = new Font((“LPK-Marathi”), 14.0f, FontStyle.Regular, GraphicsUnit.Pixel);
}
}
i have getting the problem to set font to selected controls at runetime, the font is set to form and form size get maximaize
}
Just create a new Font with a different size. Optionally you can specify the GraphicUnits
Font f = new Font(myFont.FontFamily,
10, //new size
GraphicsUnit.Display //Point is default if not specified
);
Very nice, please keep posting such kind of articles.
Really a genius,for text align i.e left, center,right how is it possible?