Library code snippets

Wrap text in a label control

This code allows you to wrap the text in a label. Do not use this code in the Change event of the label. It will cause a bad result or possibly and error message. Be sure that the AutoResize property of the label is set to true. If you change the lines containing the width of the label the variable Break should be changed accordingly.

Usage: lblTheLabel.Text = WrapLabel(lblTheLabel)

Public Sub WrapLabel(Text As Label)
Dim XStr As String
Dim Break As Integer
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Break = 40 ' the width to break the text
   With Text
       If .Width > 2700 Then
           Do Until .Width <= 2700
               X = 0
               Do Until X + Break >= Len(.Caption)
                   If .Width < 2700 Then Exit Do
                   Do Until InStr(Left(Right(.Caption, Len(.Caption) - X), Break), vbCr) = 0 And Left(Right(.Caption, Len(.Caption) - X), 1) <> vbLf
                       XStr = Left(Right(.Caption, Len(.Caption) - X - 1), Break)
                       X = X + 1
                   Loop
                   
                   If X > 0 Then
                       XStr = Left(Right(.Caption, Len(.Caption) - X), Break)
                   Else
                       XStr = Left(.Caption, Break)
                   End If
                   XStr = Left(XStr, InStrRev(XStr, " "))

                   If XStr = "" Then
                       Y = 1
                       Do
                           Z = 15
                           XStr = Left(Right(.Caption, Len(.Caption) - X), Break / Y - Z)
                           
                           Do Until InStr(Left(Right(.Caption, Len(.Caption) - X - Len(XStr)), 10), vbCr) = 0
                               Z = Z + Break / 1.5
                               XStr = Left(Right(.Caption, Len(.Caption) - X), Break / Y - Z)
                           Loop
                           
                           If Right(Left(.Caption, Len(XStr) + 3), 3) = "-" & vbCrLf Then
                               Y = Y + 1
                           Else
                               Exit Do
                           End If
                           Y = Y + 1
                       Loop
                       XStr = XStr & "-" & vbCrLf
                       
                       .Caption = Left(.Caption, X) & XStr & Right(.Caption, Len(.Caption) - Len(XStr) - X + 3)
                   Else
                       XStr = Left(XStr, Len(XStr) - 1) & vbCrLf
                       
                       .Caption = Left(.Caption, X) & XStr & Right(.Caption, Len(.Caption) - Len(XStr) - X + 1)
                   End If
                   
                   X = X + Len(XStr)
                   If .Width <= 2700 Then GoTo Done
               Loop
               If .Width <= 2700 Then GoTo Done
               Break = Break - 15
           Loop
Done:
       End If
   End With
End Sub

Comments

  1. 18 Jul 2008 at 21:17

    Here's my C# version 

    <code> 

    private string GetWrappedText(string Text, int LineLength)
    {

    Text = Text.Replace("\r\n", "");
    int len = Text.Length;
    int startPos = 0;
    StringBuilder sb = new StringBuilder();
    while (((len - startPos) / LineLength) >= 1)
    {

    int endpoint = Text.Substring(startPos, LineLength).LastIndexOfAny(new char[] { ',', ' ', ':' });
    sb.AppendLine(Text.Substring(startPos, endpoint));
    startPos += endpoint + 1;

    }

    sb.Append(Text.Substring(startPos));
    return sb.ToString();

    }

     

    </code>

  2. 23 Jul 2002 at 12:07
    It appears that you're limiting every wrapped label's width to 2700..? Why not pass the desired with for the label as a paramater or use the current label's width?   And, with that in mind, MS's label control has a WordWrap property which does wrap the text to the label's width.
  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Wrap text in a label control.

Leave a comment

Sign in or Join us (it's free).

Nick Avery I am as a web developer for a small company, working for a small company. I work on banking websites and verious related projects.
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!