How to wrap text in a label using C#?

Asked By 10 points N/A Posted on -
qa-featured

Hello all,

I am developing an application in C# and got stuck with this problem. I want to display information of person profile using labels. But there was one field in my database that has long content. It overlaps the form and kind of messing up the whole thing. I want the label to break the line upon hitting the given width or likely wrapping it in a certain boundary. How can I do that? Any idea? This is really annoying me.

Thank you so much.

SHARE
Best Answer by Herok Thakur
Answered By 0 points N/A #94925

How to wrap text in a label using C#?

qa-featured

Wrapping text in a label

It is not a good idea to store a dynamic data or label into a wrap text enabled labels. Although text can be wrapped in a label, you don’t have the full control on the object especially you are storing a text which is impossible to predict the length of the text and how log or high should the label comes in when that text wrapped on that label.

By the way, I will give you what you need. First to do is load the text completely on the wrap text enabled label then after loading resize the label based on your desired length or height. The problem in doing this method is you will miss some text which is not fitted on the label.

Now, here’s what I want to suggest to you, instead of wrapping text in a label, use a text box and make it looks like a label. Doing that thing is not a problem. You only have to change the text box properties so that it will look like a label. Here are some tips how to make it look like a label:

  1. BorderStyle must be None
  2. Set your BorderWidth value to zero.
  3. Make your text box ReadOnly by changing the property value with true. This makes your text box un-editable.
  4. Rows must be set to the number of rows you want to show in your form
  5. You must set your text box to multi-line, for multiple line of text to be viewed. It is in the TextMode property.
  6. And lastly, change your Wrap property value with true. This is what we need when displaying dynamic text to wrap in the textbox. Multi-line properties make your object able to handle multi line of text. This means the text goes on to the next line only but it doesn’t wrap a long line of text. So we need to set the Wrap property to true.

 

Best Answer
Best Answer
Answered By 0 points N/A #94927

How to wrap text in a label using C#?

qa-featured

Dear,

This is the solution of the Question is how to wrap text in a label using c#?

  • protected const string _newline = "rn";
  •  
  • /// <summary>
  •  
  • /// Word wraps the given text to fit within the specified width.
  •  
  • /// </summary>
  •  
  • /// <param name="text">Text to be word wrapped</param>
  •  
  • /// <param name="width">Width, in characters, to which the text
  •  
  • /// should be word wrapped</param>
  •  
  • /// <returns>The modified text</returns>
  •  
  • public static string WordWrap(string text, int width)
  •  
  • {
  •  
  •   int pos, next;
  •  
  •   StringBuilder sb = new StringBuilder();
  •  
  •   // Lucidity check
  •  
  •   if (width < 1)
  •  
  •     return text;
  •  
  •   // Parse each line of text
  •  
  •   for (pos = 0; pos < text.Length; pos = next)
  •  
  •   {
  •  
  •     // Find end of line
  •  
  •     int eol = text.IndexOf(_newline, pos);
  •  
  •     if (eol == -1)
  •  
  •       next = eol = text.Length;
  •  
  •     else
  •  
  •       next = eol + _newline.Length;
  •  
  •     // Copy this line of text, breaking into smaller lines as needed
  •  
  •     if (eol > pos)
  •  
  •     {
  •  
  •       do
  •  
  •       {
  •  
  •         int len = eol – pos;
  •  
  •         if (len > width)
  •  
  •           len = BreakLine(text, pos, width);
  •  
  •         sb.Append(text, pos, len);
  •  
  •         sb.Append(_newline);
  •  
  •         // Trim whitespace following break
  •  
  •         pos += len;
  •  
  •         while (pos < eol && Char.IsWhiteSpace(text[pos]))
  •  
  •           pos++;
  •  
  •       } while (eol > pos);
  •  
  •     }
  •  
  •     else sb.Append(_newline); // Empty line
  •  
  •   }
  •  
  •   return sb.ToString();
  •  
  • }
  •  
  • /// <summary>
  •  
  • /// Locates position to break the given line so as to avoid
  •  
  • /// breaking words.
  •  
  • /// </summary>
  •  
  • /// <param name="text">String that contains line of text</param>
  •  
  • /// <param name="pos">Index where line of text starts</param>
  •  
  • /// <param name="max">Maximum line length</param>
  •  
  • /// <returns>The modified line length</returns>
  •  
  • public static int BreakLine(string text, int pos, int max)
  •  
  • {
  •  
  •   // Find last whitespace in line
  •  
  •   int i = max – 1;
  •  
  •   while (i >= 0 && !Char.IsWhiteSpace(text[pos + i]))
  •  
  •     i–;
  •  
  •   if (i < 0)
  •  
  •     return max; // No whitespace found; break at maximum length
  •  
  •   // Find start of whitespace
  •  
  •   while (i >= 0 && Char.IsWhiteSpace(text[pos + i]))
  •  
  •     i–;
  •  
  •   // Return length of text before whitespace
  •  
  •   return i + 1;
  • }

Login/Register to Answer

Related Questions