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

RadLabel AutoSize doesn't function when not visible

3 Answers 221 Views
Label
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 06 Mar 2016, 10:20 PM

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;
02. 
03.foreach (RadLabel lpLabel in loLabels)
04.{
05.    lpLabel.AutoSize = false;
06.    lpLabel.TextWrap = false;
07.    lpLabel.Text = this.getText();
08.    lpLabel.TextAlignment = ContentAlignment.MiddleRight;
09.    lpLabel.AutoSize = true;
10. 
11.    if (lpLabel.Width > loWidth)
12.    {
13.        loWidth = lpLabel.Width;
14.    }
15.}

3 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 07 Mar 2016, 01:58 AM

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;

0
Phil
Top achievements
Rank 1
answered on 07 Mar 2016, 02:02 AM

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

0
Accepted
Hristo
Telerik team
answered on 07 Mar 2016, 05:02 PM
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";
radLabel.LoadElementTree();
Console.WriteLine(radLabel.Width);

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.

Regards,
Hristo Merdjanov
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
Tags
Label
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or