This is a migrated thread and some comments may be shown as answers.

Can't Set Font Size on HtmlTextBox on ItemDataBound event

1 Answer 159 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 16 Mar 2012, 05:14 PM
I created the following class to allow me to dynamically resize the font size of report elements.  The intention of this class is to allow me to add code to the ItemDataBound event for an element, section or report to automatically size the font to the best fit.  The code works great for a TextBox element but does not work for an HtmlTextBox.  The code gets the correct size based on the content that is provided but the report is not displayed with the updated style.  I have confirmed that the code works if it is added to the ItemDataBinding event but at this point the Value of the element has not been populated.  This scenario would only be beneficial for static content and in this case it would not be necessary to dynamically resize the font.

I am pretty sure that this is a bug with the HtmlTextBox control and/or the reporting engine.  Is there a way to accomplish what I am trying to do before this can be fixed?  Also, if this is in fact a bug when might you be able to fix it?

using System;
using System.Drawing;
using System.Linq;
using Telerik.Reporting.Processing;
 
namespace ReportSample
{
    public class ReportFont
    {
        public static void AutoSizeText(DetailSection dtl, int minFontSize, int maxFontSize)
        {
            foreach (Telerik.Reporting.Processing.ProcessingElement item in dtl.ChildElements)
            {
                if (!(item is ReportItem))
                    break;
 
                ReportItem rptItem = (ReportItem)item;
                if (TextSizingSupported(rptItem))
                {
                    AutoSizeText(rptItem, minFontSize, maxFontSize);
                }
            }
        }
 
        public static void AutoSizeText(ReportItem item, int minFontSize, int maxFontSize)
        {
            AutoSizeText(item, minFontSize, maxFontSize, false);
        }
 
        public static void AutoSizeText(ReportItem item, int minFontSize, int maxFontSize, bool forceResize)
        {
            if (!forceResize && !item.Name.EndsWith("_rf"))
                return;
 
            item.Style.Font.Size = GetFontSize(item, minFontSize, maxFontSize);
        }
 
        protected static Font GetFont(Telerik.Reporting.Drawing.Font font)
        {
            return GetFont(font.Name, font.Size.Value, font.Style);
        }
 
        protected static Font GetFont(Telerik.Reporting.Drawing.Font font, float fontSize)
        {
            return GetFont(font.Name, fontSize, font.Style);
        }
 
        protected static Font GetFont(string fontName, float fontSize, FontStyle style)
        {
            FontFamily fontFamily = new FontFamily(fontName);
            return GetFont(fontFamily, fontSize, style);
        }
 
        protected static Font GetFont(FontFamily fontFamily, float fontSize, FontStyle style)
        {
            return new Font(fontFamily, fontSize, style);
        }
 
        protected static bool TextSizingSupported(ReportItem item)
        {
            if (item is HtmlTextBox || item is TextBox)
                return true;
            return false;
        }
 
        protected static Telerik.Reporting.Drawing.Unit GetFontSize(ReportItem item, int minFontSize, int maxFontSize)
        {
            Size textSize;
            Font newFont = GetFont(item.Style.Font, maxFontSize);
            Size bounds = GetBounds(item);
            string value = GetValue(item);          
             
            do
            {
                textSize = System.Windows.Forms.TextRenderer.MeasureText(value, newFont, bounds, System.Windows.Forms.TextFormatFlags.WordBreak);
                if (textSize.Height > bounds.Height && newFont.Size != minFontSize)
                    newFont = GetFont(item.Style.Font, newFont.Size - 1);
            }
            while (textSize.Height > bounds.Height && newFont.Size > minFontSize);
 
            return Telerik.Reporting.Drawing.Unit.Point(newFont.Size);
        }
 
        protected static Size GetBounds(ReportItem tb)
        {
            Size bounds = (Size)tb.Bounds.Size;
            // Adjust the bounds slightly
            bounds.Height -= 3;
            return bounds;
        }
 
        protected static string GetValue(ReportItem item)
        {
            if (item is HtmlTextBox)
                return ((HtmlTextBox)item).Value.ToString();
 
            if (item is TextBox)
                return ((TextBox)item).Value.ToString();
 
            throw new Exception("Unsupported ReportItem passed.");
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Elian
Telerik team
answered on 21 Mar 2012, 04:42 PM
Hi Kevin,

This is not considered a bug. It is implemented in this way on purpose and there are various reasons that do not allow to do it otherwise. 
However, that should not be a problem for you since you can work around this issue in couple of ways:
  1. Use the ItemDataBinding event and calculate the expression manually using the DataObject of the report item.
  2. Use Bindings and a User Function to set the size (the user function will be called with the evaluated expression)
All the best,
Elian
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
Tags
General Discussions
Asked by
Kevin
Top achievements
Rank 1
Answers by
Elian
Telerik team
Share this question
or