System.Web.UI.WebControls Namespace
Represents the font properties for text.
FontInfo encapsulates the properties that specify the appearance of a font.
The example below shows how to programmatically set and retrieve a Web control's Font properties at run time, depending on user input. The code also demonstrates how simple it is to retrieve the available system fonts and dynamically add each to a selectable list.
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
InstalledFontCollection installedFonts = new InstalledFontCollection ( );
FontFamily [ ] fonts = installedFonts.Families;
foreach ( FontFamily f in fonts ) {
fontsSelect.Items.Add ( f.Name );
}
for ( int i=10; i<=20; i+=2 ) {
sizeSelect.Items.Add ( i.ToString ( ) );
}
sizeSelect.SelectedIndex = 1;
}
greeting.Font.Name = fontsSelect.SelectedItem.Text;
greeting.Font.Size = int.Parse ( sizeSelect.SelectedItem.Text );
displayFontInfo ( );
}
void displayFontInfo ( ) {
lblFontInfo.Text = greeting.Font.ToString ( );
if ( cbBold.Checked ) lblFontInfo.Text += " Bold";
if ( cbItalic.Checked ) lblFontInfo.Text += " Italic";
if ( cbOverline.Checked ) lblFontInfo.Text += " Overline";
if ( cbStrikeout.Checked ) lblFontInfo.Text += " Strikeout";
if ( cbUnderline.Checked ) lblFontInfo.Text += " Underline";
}
void setFont ( Object src, EventArgs e ) {
greeting.Font.Name = fontsSelect.SelectedItem.Value;
greeting.Font.Size = int.Parse ( sizeSelect.SelectedItem.Text );
greeting.Font.Bold = cbBold.Checked;
greeting.Font.Italic = cbItalic.Checked;
greeting.Font.Overline = cbOverline.Checked;
greeting.Font.Strikeout = cbStrikeout.Checked;
greeting.Font.Underline = cbUnderline.Checked;
}
Show me
WebControl.Font