Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.08 Adding the FontClass

Adding a FontClass to a Report Object

Every report object that displays data, whether it is dynamic or static information, uses a FontColor object to store the font related information. Instantiating a FontClass object and assigning properties to it is very trivial. The only unusual part is that it breaks out the font properties into two categories. The first category includes the standard font properties that you find in any Windows application: font name, font size, bold, and italic. For these properties, there is a separate Font object that must be instantiated and assigned to the FontColor.Font property. The second category includes the properties that are unique to Crystal Reports objects. Some of these include the Color, CSS Class and conditional formulas. These have their own properties within the FontColor class.

The following code listing is a generic method for creating a new FontClass object and populating the standard font properties. The argument list in the method declaration lets the user pass in the formatting options they want to set. The method returns a FontClass object that can be assigned to the FontClass property of any report object.

Listing 20-6. Creating a FontClass object and setting its properties.
[VB.NET]
Public Shared Function NewFontColor(ByVal Size As Integer, ByVal Name As String, ByVal Bold As Boolean, ByVal Italic As Boolean) As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
Dim myFont As CrystalDecisions.ReportAppServer.ReportDefModel.Font
myFont = New CrystalDecisions.ReportAppServer.ReportDefModel.FontClass()
myFont.Size = Size
myFont.Name = Name
myFont.Bold = Bold
Dim myFontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
myFontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColorClass()
myFontColor.Font = myFont
Return myFontColor
End Function
[C#]
public static CrystalDecisions.ReportAppServer.ReportDefModel.FontColor NewFontColor(int Size, string Name, bool Bold, bool Italic)
{
CrystalDecisions.ReportAppServer.ReportDefModel.Font myFont;
myFont = new CrystalDecisions.ReportAppServer.ReportDefModel.FontClass();
myFont.Size = Size;
myFont.Name = Name;
myFont.Bold = Bold;
CrystalDecisions.ReportAppServer.ReportDefModel.FontColor myFontColor;
myFontColor = new CrystalDecisions.ReportAppServer.ReportDefModel.FontColorClass();
myFontColor.Font = myFont;
return myFontColor;
}