Click here to Skip to main content
Click here to Skip to main content

Change Font & Size of Crystal Report Dynamically

By , 2 Nov 2011
Rate this:
Please Sign up or sign in to vote.

Introduction

This article describes how to change font and size of crystal report dynamically (runtime) in VB.NET 2005.

Using the Code

Open a new Windows application project using VB.NET and add a button, CrystalReportViewer from the Toolbox. Adding the control and dropping it on the form design it as shown in fig.

then add a SQL Database name as “Database1.mdf” create a table name as “Fonts” with 3 fields : FontName, Size, SrNo and set SrNo field value as 1

Imports System.Data.SqlClient
 
Public Class Form1
 
    Dim rpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    'path variable use for Get application running path
    Dim path As String = (Microsoft.VisualBasic.Left(Application.StartupPath, Len(Application.StartupPath) - 9))
    'connetiong string
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & path & "Database1.mdf;Integrated Security=True;User Instance=True")
    Dim cmd As SqlCommand
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FontDialog1.ShowDialog()
        'Update query is fired 
        cmd = New SqlCommand("UPDATE Fonts SET FontName ='" & FontDialog1.Font.Name.ToString & "',Size = " & FontDialog1.Font.Size.ToString & " WHERE SrNo = 1", con)
        cmd.ExecuteNonQuery()
        'Function report call
        report()
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.Open()
        'Function report call
        report()
    End Sub
    Sub report()
        On Error Resume Next
        Dim dset As New DataSet
        Dim adp As New SqlDataAdapter("Select * from Fonts", con)
        adp.Fill(dset, "Fonts")
        'path variable store a runnig application path"
        rpt.Load(path & "CrystalReport1.rpt")
        rpt.SetDataSource(dset)
        rpt.Refresh()
        CrystalReportViewer1.ReportSource = rpt
    End Sub
 
End Class
Create a crystal report and add field from Database FontName & Size then Add Formula Fields name as “font”.Edit the Formula Field and use html font tag set it attributes face & size save it

The HTML tag is used in crystal report to set a font

Drop the formula field on crystal report then right click on it: Format Object -> Paragraph -> text interpretation -> HTML text -> OK button.

Limitations

Some font are not supporting to Html tag ,font size will be set 1 to 7 only. This is a drawback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Patil Kishor
Software Developer
India India
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 3 Nov 2011
Article Copyright 2011 by Patil Kishor
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid