Set The Size of TextBox Dynamically in Telerik Report

5 posts, 0 answers
  1. Negin
    Negin avatar
    8 posts
    Member since:
    Jun 2012

    Posted 28 Jan 2013 Link to this post

    Hi,

    Is there any way to set the size of a textbox dynamically based on length of its text in Telerik Reports?

    Thanks.
  2. Peter
    Admin
    Peter avatar
    1665 posts

    Posted 30 Jan 2013 Link to this post

    Hello Negin,

    Out of the box the TextBox item can only grow vertically downwards pushing any other items below it. 
    However you can use some custom logic to calculate the textbox width based on value length and set it with Binding. Additionally our suggestion is to use a monospaced font.

     Property path

    Expression 

     Size

    =CalculateSize(ReportItem)


    public static Telerik.Reporting.Drawing.SizeU CalculateSize(Telerik.Reporting.Processing.TextBox textBox)
    {
        int len = textBox.DataObject["Name"].ToString().Length;
        double width = len * 6; //this is pseudo calculation
        return new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Pixel(width), textBox.Size.Height);
    }

    All the best,
    Peter
    the Telerik team

    HAPPY WITH REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!

  3. Negin
    Negin avatar
    8 posts
    Member since:
    Jun 2012

    Posted 31 Jan 2013 Link to this post

    Thanks a lot,

    Negin
  4. Alexandre
    Alexandre avatar
    1 posts
    Member since:
    Jun 2019

    Posted 10 Oct in reply to Peter Link to this post

    Hello,

    How do you make this code working :

    public static Telerik.Reporting.Drawing.SizeU CalculateSize(Telerik.Reporting.Processing.TextBox textBox)
    {
        int len = textBox.DataObject["Name"].ToString().Length;
        double width = len * 6; //this is pseudo calculation
        return new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Pixel(width), textBox.Size.Height);
    }

    I try to modify font size of a textbox dynamically depending of text length but when I use this user function, I have and error message : 

    System.NullReferenceException : 'Object reference not set to an instance of an object.'
    Telerik.Reporting.Processing.IDataObject.this[string].get retournée null.

    I have set the binding as describe above and when I debug I have my textbox which is passed to the function. I don't understand why I can't get the value of the textbox. For information, i'm passing an object to the report and the textbox is binded to a field of this object. I execute RenderReport ( To generate a PDF ).

    Do you have an idea ?

    Thank you

  5. Peter
    Admin
    Peter avatar
    1665 posts

    Posted 14 Oct Link to this post

    Hello Alexandre,

    According the code the data source should provide field with name "Name". If such field is not available replace the named indexer in the code snippet with the available field in this line:

    int len = textBox.DataObject["Name"].ToString().Length;

    Regards,
    Peter
    Progress Telerik

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top