Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create programatically some textboxes, and I want to put some text in them. But they don't display the whole text, I have to click and drag to left to show all text..

Isn't there an option, maybe "AutoSize", "AutoFit" or something like that?

Thank you!
Posted

No - there is no standard way to change either the size of the text box, or the font size to fit the text into the display.
However, if you make it multi line and enable ScrollBars it can do something like what you want.

If you must have a re-sizing control, then I suspect you will have to write it yourself.
 
Share this answer
 
You can use TextRenderer http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.aspx[^] to determine what size the text box needs to be in the TextChanged event of the textbox - or better yet do as OriginalGriff suggests and make your own re-sizing custom control inheriting from System.Windows.Forms.TextBox
Example
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextBox t = (TextBox)sender;
    Size s = TextRenderer.MeasureText(t.Text, t.Font);
    t.Width = s.Width;
}
 
Share this answer
 
in text box properties change the text mode(single line->multiline)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900