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

Posted on 2007-07-19
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.
Question by:lapucca
    LVL 8

    Expert Comment

    You acheive this using a CSS class
    word-wrap: break-word

    LVL 6

    Accepted Solution

    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.

    LVL 3

    Expert Comment

    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.
    LVL 3

    Expert Comment

    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)


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now