RadLabel AutoSize doesn't function when not visible

4 posts, 1 answers
  1. Phil
    Phil avatar
    15 posts
    Member since:
    Oct 2012

    Posted 06 Mar Link to this post

    I'm using 2016 Q1 (Jan) and am dynamically adding RadLabels to a user control. I have converted from .NET Label to RadLabel.

    The user control fills a SplitPanel and is currently out of focus when the form first opens.

    The form is always visible and is loaded before the RadLabels are dynamically added.

    When dynamically adding the RadLabels they are each added to the user control, their text is set and then they are all adjusted and aligned to match the length of the RadLabel with the longest text. Well, that's how it used to work with the normal .NET Label...

    Each RadLabel is set correctly but none ever resize to fit their text and instead are always 100 wide even though some should be over 200.

    I've tried implementing the AutoSize to false, set text, AutoSize to true workaround but it never works. No combination of AutoSize makes any difference.

    I cannot apply these changes in other place due to the nature of the code (plugins, etc.) so they must take effect here.

    Is there a way to get the calculated size of the RadLabels and use that width? Is there a way to get the RadLabel to behave as expected?


    This snippet takes place after the RadLabels have all been added to the user control and I need to get the longest width.

    01.Int32 loWidth = 0;
    03.foreach (RadLabel lpLabel in loLabels)
    05.    lpLabel.AutoSize = false;
    06.    lpLabel.TextWrap = false;
    07.    lpLabel.Text = this.getText();
    08.    lpLabel.TextAlignment = ContentAlignment.MiddleRight;
    09.    lpLabel.AutoSize = true;
    11.    if (lpLabel.Width > loWidth)
    12.    {
    13.        loWidth = lpLabel.Width;
    14.    }

  2. Phil
    Phil avatar
    15 posts
    Member since:
    Oct 2012

    Posted 06 Mar Link to this post

    Another issue is that if I do set a very large Width by default the RadLabel doesn't align the text properly. It stays left aligned even though I explicitly set it.

    1.lpLabel.TextAlignment = ContentAlignment.MiddleRight;

  3. DevCraft R3 2016 release webinar banner
  4. Phil
    Phil avatar
    15 posts
    Member since:
    Oct 2012

    Posted 06 Mar Link to this post

    With AutoSize = true I've tried calculating the expected value in a very rough way using this code snippet which somehow forces the RadLabel to evaluate its size and I'm then able to retrieve the Width accurately afterwards simply by access the normal RadLabel.Width property.

    1.lpLabel.CreateGraphics().MeasureString(lpLabel.Text, lpLabel.Font).Width

  5. Answer
    Hristo Merdjanov
    Hristo Merdjanov avatar
    664 posts

    Posted 07 Mar Link to this post

    Hi Phil,

    Thank you for writing.

    This behavior is caused by the specifics of the TPF building our controls from lightweight visual elements. In this particular case, the labels would not have a calculated size until the control is shown. You have already found a possible solution by measuring the text of the label.

    Another solution would be to manually force the layout engine to perform a measure of the label at the moment when you are creating it. This can be accomplished by calling the LoadElementTree method:
    RadLabel radLabel = new RadLabel();
    radLabel.Text = "Some Text";

    I am sending you attached a sample project demonstrating the suggested approach.

    I hope this helps. Please let me know if you need further assistance.

    Hristo Merdjanov
    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