AfricaDotNet
www.AfricaDotNet.com Continues the .NET Conversation in Africa

Today's Byte # 2 : Font Manipulation

rated by 0 users
This post has 0 Replies | 1 Follower

Top 10 Contributor
Posts 40
Rad Posted: 07-29-2009 2:56 AM

In the course of your development, you might find that you need to change the font style of a control to somthing like bold or italic.

Upon investigation, you find that each control has a Font property, that additionally has a boolean Bold property. So you would attempt the following:

 

   1: //
   2: // Set the font to bold
   3: //
   4: txtName.Font.Bold = true;

 

This unfortunately, would fail. Why? Because the Bold property of a font is read only.

To change the font to bold, you actually need to create a new font, and specify that the new font be created with the style bold. Like so

 

   1: //
   2: // Change the font to bold, using the existing font
   3: //
   4: txtName.Font = new Font(txtName.Font, FontStyle.Bold);

 

The same thing would apply for italics.

 

   1: //
   2: // Change the font to italic, using the existing font
   3: //
   4: txtName.Font = new Font(txtName.Font, FontStyle.Italic);

 

An intersting thing to note -- you can also combine the multiple styles. Like so

 

   1: //
   2: // Change the font to bold, strikeout & italic , using the existing font
   3: //
   4: txtName.Font = new Font(txtName.Font, FontStyle.Italic | FontStyle.Strikeout | FontStyle.Bold);

 

Page 1 of 1 (1 items) | RSS
(c) AfricaDotNet
Powered by Community Server (Non-Commercial Edition), by Telligent Systems