Library code snippets
Wrap text in a label control
By Nick Avery, published on 28 Feb 2002
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
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
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...
<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)
{
}
sb.Append(Text.Substring(startPos));return sb.ToString();
}
</code>
This thread is for discussions of Wrap text in a label control.