Hi Guys,
This is driving me daft, I need to restrict the length of each line entered onto a multi line textbox to only be X chars, however the number of lines is not a concern.
Now i have found this code on internet which needed a little tweaking but as it stands now ...
This code works great provided that the string does not already contain line breaks eg
if I enter the following, assuming i want the maximum length of each line to be 10 chars
the result is
However what I actually want is for it too take into account the line breaks I have entered already in the string and then add new ones where appropriate, i.e.
Obviously this is because the very first line of the code strips out line breaks/ carriage returns, if I remove that line it puts in extra line breaks so the text gets further and further apart !
I've already tried a few ways of modifying the code but am out of ideas
Does anyone have any suggestions?
Cheers,
Daniel
This is driving me daft, I need to restrict the length of each line entered onto a multi line textbox to only be X chars, however the number of lines is not a concern.
Now i have found this code on internet which needed a little tweaking but as it stands now ...
Code:
Public Function Wrap(ByVal text As String, ByVal maxLength As Integer) As String
text = text.Replace(vbCrLf, " ")
Dim Words() As String = text.Split(" ")
Dim currentLineLength As Integer = 0
Dim Lines As ArrayList = New ArrayList((text.Length / maxLength))
Dim currentLine As String = ""
Dim InTag As Boolean = False
For Each currentWord As String In Words
If (currentLineLength + (currentWord.Length + 1)) < maxLength Then
currentLine = currentLine & " " & currentWord.Trim(" ")
currentLineLength = currentLineLength + (currentWord.Length + 1)
Else
Lines.Add(currentLine)
currentLine = currentWord
currentLineLength = currentWord.Length
End If
Next
If (currentLine <> "") Then
Lines.Add(currentLine)
End If
Dim textLinesStr() As String = New String((Lines.Count) - 1) {}
Lines.CopyTo(textLinesStr, 0)
Dim strReturn As String = ""
For i As Integer = 0 To textLinesStr.Count - 1
textLinesStr(i).Trim(vbCrLf)
textLinesStr(i).Trim(" "c)
strReturn = strReturn & textLinesStr(i) & ControlChars.NewLine
Next
Return strReturn.Trim(" "c)
End Function
This code works great provided that the string does not already contain line breaks eg
if I enter the following, assuming i want the maximum length of each line to be 10 chars
Code:
This Line is too long
I am Line2
Hello3
xyz
the result is
Code:
This Line is too long I am
Line2 Hello3 xyz
However what I actually want is for it too take into account the line breaks I have entered already in the string and then add new ones where appropriate, i.e.
Code:
This Line
is too long
I am Line2
Hello3
xyz
Obviously this is because the very first line of the code strips out line breaks/ carriage returns, if I remove that line it puts in extra line breaks so the text gets further and further apart !
I've already tried a few ways of modifying the code but am out of ideas
Does anyone have any suggestions?
Cheers,
Daniel