How to turn wrapped text into an array

If I have a text area with wrapped text, what is the best way to create an array of words that correctly represents the wrapped text?

eg Someone enters “I wandered lonely as a cloud”
The textarea displays

I wandered lonely as a cloud

But the .text property has no endofline characters.
How best to end up with

line(0) = “I wandered”
line(1) = “lonely as a”
line(2) =“cloud”

LineNumAtCharPos is your friend.

Unfortunately, it has bugs which as far as I know haven’t been fixed yet.

If you look at feedback://showreport?report_id=51461 I did include a workaround for the bug I hit.

Here is a function that might help.

I used this to take some arbitrary text put it into a hidden edit field (pEditField) so that I could determine the individual line strings to draw.
• kcr is a constant that maps to Chr(13)
• pText is the string that needs splitting.
• pMaxWidth is used to resize the hidden edit field to the maximum width of the area I will be drawing into later on.
• pEditField is the hidden edit field.

I imagine you won’t need pText & pMaxWidth.

[code]Function WrapText(pText As String, pMaxWidth As Int32, pEditField As TextArea) As String()
Dim theResult(-1) As String
Dim nextLine As Int32
Dim lineNumber As Int32
Dim textLine As String
Dim i As Int32
Dim theChar As String

If pEditField.Width <> pMaxWidth Then
pEditField.Width = pMaxWidth
End If

pText = ReplaceLineEndings(pText, kcr)

pEditField.Text = pText

lineNumber = 0
textLine = “”
For i = 1 To Len(pText)
theChar = Mid(pText, i, 1)

'if the character is a carriage return then add the line and reset the entry field
'this prevents a problem under win32 where carriage returns seem to effect the wrapping
If theChar = kcr Then
  theResult.Append(Trim(textLine))
  
  textLine = ""
  lineNumber = 0
  
  pText = Mid(pText, i + 1)
  pEditField.Text = pText
  
  i = 0
Else
  nextLine = pEditField.LineNumAtCharPos(i)
  If nextLine > lineNumber Then
    'osx seems to report the change one character too late (bug?) so we need to break at the previous character
    #If TargetMacOS Then
      theResult.Append(Trim(Left(textLine, Len(textLine) - 1)))
      
      i = i - 1
      theChar = Mid(pText, i, 1)
    #Else
      theResult.Append(Trim(textLine))
    #EndIf
    
    textLine = ""
    lineNumber = nextLine
  End If
  
  textLine = textLine + theChar
End If

Next

If Len(textLine) > 0 Then
theResult.Append(Trim(textLine))
End If

Return theResult
End Function[/code]

Thanks.
Im getting odd results at the moment but will persevere! :slight_smile: