TextEdit.LineNumAtCharPos

From Xojo Documentation

Method

TextEdit.LineNumAtCharPos(CharPosition as Integer) As Integer

Supported for all project types and targets.

Returns (as an Integer) the line number in which the CharPosition character appears.

Notes

Characters are numbered consecutively with the first character numbered 1. The first line is numbered zero. Used with ScrollPosition, this lets you scroll the control to a particular place in the text.

The line number reflects line breaks from both hard line breaks in the text and soft line breaks caused by word-wrapping within the control.

Example

Gets the line number for the character at position 100:

Dim lineNum As Integer = TextArea1.LineNumAtCharPos(100)

This example searches a TextArea using text provided in a TextField and then scrolls to the text in the TextArea:

Dim searchText As String = TextField1.Text
Dim charPos As Integer = InStr(TextArea1.Text, searchText)

If charPos > 0 Then
TextArea1.ScrollPosition = TextArea1.LineNumAtCharPos(charPos)
Else
// InStr returns 0 if no match was found.
End If