Search code examples
vb.netcrystal-reportscrystal-reports-2008

How to change Crystal Report font Dynamically using vb.net


I am using Visual Studio 2013 with crystal report 13.0.12.1494. In My Project I have a Crystal Report With fields from database (SQL Server as Database). I am storing reference to font in project settings and want to replace font according to user preference from project settings for e.g. crystal report built with font arial, 10pt and user chooses times new roman, 12pt at runtime then report should be shown in times new roman, 12pt.

I tried Following without success

Dim myparam As New ParameterField
Dim myDiscreteValue As New ParameterDiscreteValue
myparam.ParameterFieldName = "My Parameter"
myDiscreteValue.Value = My.Settings.MyFont.Name

Thanks in Advance


Solution

  • Here is the c# i used to change dynamically the font of my object.

    The text object can be TextObject ( simple text ) or FieldObject ( from db )

     public static void ApplyFontAllText(ReportDocument rapport, Font style)
        {
    
            foreach (ReportObject obj in rapport.ReportDefinition.ReportObjects)
            {
                if (obj.GetType().Name.Equals("TextObject"))
                {
    
                  ((TextObject)obj).ApplyFont(style);
    
                }
                else if (obj.GetType().Name.Equals("FieldObject"))
                {
                    ((FieldObject)obj).ApplyFont(style);
                }
            }
        }
    

    The both class have a ApplyFont method. You can parse your font and then use ApplyFont.