[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I make a label control wrap text after I assign the max. size it could have?

Posted on 2007-07-19
5
Medium Priority
?
5,468 Views
Last Modified: 2008-01-09
Hi, I'm using VS2005, .net 2, C# for windows application.
How can I make a label control wrap text after I assign the max. size it could have?  Thanks.
0
Comment
Question by:lapucca
5 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 19528124
You acheive this using a CSS class
.word
{
word-wrap: break-word
}


0
 
LVL 6

Accepted Solution

by:
Praesidium earned 2000 total points
ID: 19528827
When label is set to AutoSize = true, it measures as if all the text wants to be on one line.  In order to have multiple lines of text in 1.1, the AutoSize property cannot be used.
The Label control now has a GetPreferredSize() method which takes wrapping constraints.  This method will take out all the headaches of guessing by taking into account all the details you might not know about, e.g. non-client borders/padding and the technology used to draw text.

More:
http://blogs.msdn.com/jfoscoding/articles/478299.aspx
0
 
LVL 3

Expert Comment

by:Nico
ID: 19529415
I used the following solution.. but it doesn't offer nice word wrapping I suppose:

Label label = new Label();
label.Text = "this is a veeeeeery long string! Well.. actually it isn't, but it's long enough for this example...";
int charactersFitted, linesFilled;
Graphics g = label.CreateGraphics();
// measure the width of the string.. the size given is the maximum width for the label and a large enough height
g.MeasureString(label.Text, Font, new SizeF(label.Width, 9999), StringFormat.GenericTypographic, out charactersFitted, out linesFilled);
label.Height = Font.Height * linesFilled + 2;

As Praesidium stated, a GrePreferredSize() solution might be a possibility too, but I have no experience and no example of this.
0
 
LVL 3

Expert Comment

by:Nico
ID: 19529528
Hmm, yup, GetPreferredSize would also work.

Label label = new Label();
Size maxSize = new Size(100, 100);
label.Text = "this is a veeeeeery long string! Well.. actually it isn't, but it's long enough for this example...";
label.Size = label.GetPreferredSize(maxSize);

This actually a lot less code :P *uses code in project*

Anyway, the GetPreferredSize method calculates the size that it needs to display its text (or whatever is on the label), but it heeds the maximum size you want to give it (it's actually called 'proposedSize', so I'm not sure how aaccurate this actually would be)

0
 

Author Comment

by:lapucca
ID: 19533141
I turned off the AutosSize and then in designer, I was able to click and drag border of the label box.  That' it.  It does the text wrapping automatically with in the size I made.  thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question