Auto size for textbox

4 posts, 1 answers
  1. Sivam
    Sivam avatar
    37 posts
    Member since:
    Jan 2010

    Posted 05 May 2010 Link to this post

    Hi,
       I want to have the textbox to be auto-sized. I want the size to grow depends on the value of the textbox. Does this achievable? I can specify the max width, afterwhich it should wrap. How to get these?

    Thanks in advance.
  2. Answer
    Peter
    Admin
    Peter avatar
    1665 posts

    Posted 07 May 2010 Link to this post

    Hello Sivam,

    If you need the item to wrap the text and grow vertically when the the text is too long to fit on a single line, this is easy achievable. Just set the CanGrow property of the TextBox to true.

    If you want to dynamically change the width of the item, you will need your custom logic to somehow determine some width.

    Then you have to possible solutions:

    1. With an event as shown in the following code snippet to programmatically set a TextBox width:

    private void textBox1_ItemDataBound(object sender, EventArgs e)
    {
        var procTextBox = (sender as Telerik.Reporting.Processing.TextBox);
     
        if (procTextBox.Value.ToString().Length> 5)
        {
            procTextBox.Width = Unit.Pixel(50);
        }
        else
        {
            procTextBox.Width = Unit.Pixel(5);
        }
    }

    2. Set the Bindings Width property to an User Function as shown in following code snippet:

    public static Telerik.Reporting.Drawing.Unit RunTimeWidth(string value)
    {
        if (value.Length > 5)
        {
            return Unit.Pixel(200);
        }
        else
        {
            return Unit.Pixel(5);
        }
    }

    Hope this information helps.
    Best wishes,
    Peter
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. Satish
    Satish avatar
    11 posts
    Member since:
    Jul 2012

    Posted 22 Aug 2012 Link to this post

    Hello
              I want to have the textbox to be auto-sized. I want the size to grow depends on the value of the textbox. Does this achievable? 
    Here i attached the screen shot in that the value first column header is surname1234567890.when execute the report it shows surname only,1234567890 is under second column header called first_name.How to display surname1234567890 then next column header should display



    public partial class Report3 : Telerik.Reporting.Report
        {
            private Unit oneunit = new Unit(10 / 8);
            private Unit originalsurnameCaptionTextBox;
            public Report3()
            {
                //
                // Required for telerik Reporting designer support
                //
                InitializeComponent();
                originalsurnameCaptionTextBox = surnameCaptionTextBox.Width;


                surnameCaptionTextBox.ItemDataBound += new EventHandler(surnameCaptionTextBox_ItemDataBound);


                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
            void surnameCaptionTextBox_ItemDataBound(object sender, EventArgs e)
            {
                Telerik.Reporting.Processing.TextBox surnameCaptionTextBox = (Telerik.Reporting.Processing.TextBox)sender;
                ////surnameCaptionTextBox.Style.BackgroundColor = Color.Purple;
                //surnameCaptionTextBox.Width = originalsurnameCaptionTextBox * (surnameCaptionTextBox.Text.Length);
                ////var surnameCaptionTextBox = (sender as Telerik.Reporting.Processing.TextBox);


                if (surnameCaptionTextBox.Value.ToString().Length > 15)
                {
                    surnameCaptionTextBox.Width = Unit.Pixel(150);
                }
                else
                {
                    surnameCaptionTextBox.Width = Unit.Pixel(5);
                }
            
            }
        }
    }
  4. Peter
    Admin
    Peter avatar
    1665 posts

    Posted 24 Aug 2012 Link to this post

    Hi Satish,

    Out of the box the TextBox item can only grow vertically downwards pushing any other items below it. 
    However as you have tried you can use some custom logic to calculate the textbox width based on value length and set it with Binding.

     Property path

    Expression 

     Size

    =CalculateSize(ReportItem)

    To calculate the size you can use an User Function:

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

    Currently as best practice our suggestion is to avoid report events and instead utilize declarative approach with Conditional Formatting and Bindings

    All the best,
    Peter
    the Telerik team

    BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

Back to Top