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

Autosizing a RadTextBoxElement to its text

1 Answer 136 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 2
Michael asked on 02 Apr 2009, 09:44 PM
Hello -

I'm trying to cause a RadTextBoxElement to automatically size to its text, similar to the way a TextPrimitive automatically sizes to its text.  Here is the code snippet:

        public Form1() 
        { 
            StackLayoutPanel panel = new StackLayoutPanel(); 
            panel.Orientation = Orientation.Horizontal; 
            panel.AutoSize = true
            panel.Children.Add(new TextPrimitive() { Text = "Color:" }); 
            panel.Children.Add(new RadTextBoxElement() { Text = "Purple" }); 
            RadPanel elementsContainer = new RadPanel(); 
            elementsContainer.ThemeName = "Desert"
            elementsContainer.PanelElement.Children.Add(panel); 
            Controls.Add(elementsContainer); 
        } 
 
 

The TextPrimitive properly sizes to its text ("Color:"), but the RadTextBoxElement fails to size to its text ("Purple").  If I set AutoSize = true on the RadTextBoxElement, nothing changes.  If I also set the AutoSizeMode to WrapAroundChildren, still no change.  Can you advise me on how to make this work?

Thanks,
Michael Hewitt
Precision Software Design, LLC

1 Answer, 1 is accepted

Sort by
0
Accepted
Mike
Telerik team
answered on 03 Apr 2009, 03:22 PM
Hello Michael,

RadTextBox (respectively RadTextBoxElement) control uses internally the standard TextBox control (mainly due to its complexity). This means RadTextBoxElement has the same limitations as TextBox.
You can control whether the text box should appear on a single or multiple lines by settings the Multiline property (radTextBoxElement.TextBoxItem.Multiline)

If you need to "autosize" the text box according to the text, you shoud set Multiline=true and set TextBoxElement size, corresponding to the measured text rectangle. You can use the library method:
System.Windows.Forms.TextRenderer.MeasureText

Let me know if this works for you.

All the best,
Mike
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
Michael
Top achievements
Rank 2
Answers by
Mike
Telerik team
Share this question
or