C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Winforms Controls »

Calculating width of TextBox dynamically based on Text in C#


Posted Date: 20 Jun 2009    Resource Type: Code Snippets    Category: Winforms Controls
Author: ABitSmartMember Level: Diamond    
Rating: 1 out of 5Points: 10 (Rs 5)



Sometimes we need to expand the TextBox dynamically based on the length of the entered text. TextBox does not have a AutoSize property by which it can adjust it's size based on the content. On way of calculating the Width for the TextBox is using the Graphics.MeasureString and calculating the Pixel width. This method has it's Pros and cons.

A quick and easy (cheeky!!) method to do the same thing is to assign the text to a Label control's Text property. With AutoResize of the label set to true, the Label's width will automatically widen to fit the Text. Then use the Label's width to set the width for the TextBox.


Label lbl = new Label();
lbl.AutoSize = true;
lbl.Text = yourtextbox.Text;
yourTextbox.Width = lbl.Width;


Have fun.

For more details, visit Http://abitsmart.com/?p=101



Responses

Author: Abhay    05 Nov 2009Member Level: Diamond   Points : 1
Hi

It's showing error there is no property AutoSize .Please check it and put the valid URL.


Author: ABitSmart    05 Nov 2009Member Level: Diamond   Points : 1
Huh?? What 'valid link' are you referring to ?

MSDN - http://msdn.microsoft.com/en-us/library/
system.windows.forms.label.autosize.aspx




Author: Mohan    05 Nov 2009Member Level: Diamond   Points : 1
you need to declare like this..

System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();






Author: Abhay    05 Nov 2009Member Level: Diamond   Points : 1
I am asking about this link that you have provided

For more details, visit C#;Winforms;Textbox width;Dynamically set TextBox width

And there is no property found as Autosize for a label.




Author: ABitSmart    05 Nov 2009Member Level: Diamond   Points : 1
I have fixed the link. Check my first reply, it has a link to MSDN. Have you had a look at it ?

Pasting the link on this reply again,
http://msdn.microsoft.com/en-us/library/
system.windows.forms.label.autosize.aspx


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Winforms  .  Textbox width  .  Http://abitsmart.com/?p=101  .  Dynamically set TextBox width  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: how to pass variable between forms
Previous Resource: Opening a Website from Winforms
Return to Discussion Resource Index
Post New Resource
Category: Winforms Controls


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use