|
How do I change the Font of Field objects and Text objects in a Crystal report Programatically ?
How do I let the user change the Font of Field objects and Text objects in a Crystal report using VB/RDC/CRAXDRT?
The article shows you how to let the user change Font properties of the Field and Text objects in a Crystal report.
The article demonstrates it using the CRAXDRT object Library inconjection with the ShowFont Commondialog control and
IFontDisp object.
First of all, Reference Crystal Reports ActiveX Designer Run Time Library to your VB project.
Though I've used version 9.0 objects in the article, it should work with version 8/8.5/9/10/XI without any trouble, I think.
Then add a Microsoft Common Dialog Control and a Crystal Report Viewer Control to your Form from the components dialog box.
First, let's declare the Object variables to hold the Crystal Application, Report and Font objects
Dim CrxApp As CRAXDRT.Application
Dim crxReport As CRAXDRT.Report
Dim reportObject As Object
Dim myFont As IFontDisp 'Font Object.
Dim I As Integer
Dim X As Integer
Instantiate the Application object and open the Crystal Report from the disk
Set CrxApp = New CRAXDRT.Application
Set crxReport = CrxApp.OpenReport("H:\test\crtests\Report1.rpt")
Initialize the Font object by populate it with the Font of the Form
Set myFont = Me.Font
Set what fonts to display in the ShowFont dialog box
CommonDialog1.Flags = (cdlCFBoth Or cdlCFEffects)
Show the Font dialog box
CommonDialog1.ShowFont
Set properties of the Font Object to what the user selected
myFont.Bold = CommonDialog1.FontBold
myFont.Italic = CommonDialog1.FontItalic
myFont.Name = CommonDialog1.FontName
myFont.Size = CommonDialog1.FontSize
myFont.Underline = CommonDialog1.FontUnderline
myFont.Strikethrough = CommonDialog1.FontStrikethru
Now, loop through all the sections and apply Font properties to the Field and Text objects
I=1
X=1
For I = 1 To crxReport.Sections.Count
For X = 1 To crxReport.Sections(I).ReportObjects.Count
Set reportObject = crxReport.Sections(I).ReportObjects.Item(X)
If reportObject.Kind = crTextObject Or reportObject.Kind = crFieldObject Then
reportObject.Font.Name = myFont.Name
reportObject.Font.Bold = myFont.Bold
reportObject.Font.Italic = myFont.Italic
reportObject.Font.Size = myFont.Size
reportObject.Font.Underline = myFont.Underline
reportObject.Font.Strikethrough = myFont.Strikethrough
End If
Set reportObject = Nothing
Next X
Next I
Now, preview the Report
'Make sure you add a Crystal Report Viewer Control to you form
CRViewer91.ReportSource = crxReport
CRViewer91.ViewReport
Finally, release the references.
Set crxReport = Nothing
Set crxApplication = Nothing
'Add a command button and a Crystal Report Viewer Control and a Microsoft Common Dialog Control to you form
'Reference Crystal Reports 8/8.5/9/10 ActiveX Designer Run time Library
'Paste the following code into the form code window
'Change the path "C:\crtests\Report1.rpt" to suit yours
Private Sub Command1_Click()
Dim CrxApp As CRAXDRT.Application
Dim crxReport As CRAXDRT.Report
Dim reportObject As Object
Dim myFont As IFontDisp 'Declare a Font Object.
Dim I As Integer
Dim X As Integer
Set CrxApp = New CRAXDRT.Application
Set crxReport = CrxApp.OpenReport("H:\test\crtests\Report1.rpt")
'Initialize the font object. Populate it with the Font of the Form
Set myFont = Me.Font
'Set what fonts to display
CommonDialog1.Flags = (cdlCFBoth Or cdlCFEffects)
'Show the Font dialog
CommonDialog1.ShowFont
'Set properties of the Font Object to what the user selected
myFont.Bold = CommonDialog1.FontBold
myFont.Italic = CommonDialog1.FontItalic
myFont.Name = CommonDialog1.FontName
myFont.Size = CommonDialog1.FontSize
myFont.Underline = CommonDialog1.FontUnderline
myFont.Strikethrough = CommonDialog1.FontStrikethru
'now loop through all the sections and apply Font properties to field and text objects
For I = 1 To crxReport.Sections.Count
For X = 1 To crxReport.Sections(I).ReportObjects.Count
Set reportObject = crxReport.Sections(I).ReportObjects.Item(X)
If reportObject.Kind = crTextObject Or reportObject.Kind = crFieldObject Then
reportObject.Font.Name = myFont.Name
reportObject.Font.Bold = myFont.Bold
reportObject.Font.Italic = myFont.Italic
reportObject.Font.Size = myFont.Size
reportObject.Font.Underline = myFont.Underline
reportObject.Font.Strikethrough = myFont.Strikethrough
End If
Set reportObject = Nothing
Next X
Next I
'Preview the report
CRViewer91.ReportSource = crxReport
CRViewer91.ViewReport
'release the references
Set crxReport = Nothing
Set crxApplication = Nothing
End Sub
|