Asked By
victor07
10 points
N/A
Posted on - 07/28/2011
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.
How to wrap text in a label using C#?
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:
-
BorderStyle must be None
-
Set your BorderWidth value to zero.
-
Make your text box ReadOnly by changing the property value with true. This makes your text box un-editable.
-
Rows must be set to the number of rows you want to show in your form
-
You must set your text box to multi-line, for multiple line of text to be viewed. It is in the TextMode property.
-
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.
Â
How to wrap text in a label using C#?
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;
-
}