These TextBox and RichEdit control methods return information about positions in the text.
% = Object.LineCount (or RichEdit.LineCnt)
% = Object.LineFromChar(index%)
% = Object.CharFromLine(index%)
% = Object.RowFromChar(index%)
% = Object.ColFromChar(index%)
% = Object.GetLineFromChar(index%)
Object | : TextBox, RichEdit Ocx |
LineCount (property of both objects) and LineCnt (RichEdit only) retrieve the number of lines in a multiline edit control. If the edit control is empty, the return value is 1.
LineFromChar() and GetLineFromChar() retrieve the index of the line that contains the specified character index.
CharFromLine() retrieves the index of the first character of a line. The character index is the number of characters from the beginning of the edit control to the specified line.
RowFromChar() retrieves the y-coordinate of the specified character in an edit control, while ColFromChar() retrieves the x-coordinate of the specified characterl. The coordinates are relative to the left-top corner of the control. See Known Issues below.
Global n As Int32
AutoRedraw = 1
Ocx TextBox tb = "", 10, 10, 210, 200 : .MultiLine = True : .BorderStyle = 1 : .ScrollBars = 2
For n = 1 To 100 : tb.Text = tb.Text & "Box" & n & ", " : Next n
Ocx Command cmd = "Add another box", 230, 10, 100, 22
tb_Stats
Do : Sleep : Until Me Is Nothing
Sub cmd_Click
Local tbss = tb.SelStart
tb.Text = tb.Text & "Box" & n & "," : Inc n
tb.SetFocus : tb.SelStart = tbss
tb_Stats
EndSub
Sub tb_KeyUp(Code&, Shift&)
tb_Stats
EndSub
Sub tb_MouseUp(Button&, Shift&, x!, y!)
tb_Stats
EndSub
Sub tb_Stats
Local tl = tb.LineFromChar(tb.SelStart)
Text 230, 40, "Number of Lines:" & tb.LineCount & " "
Text 230, 56, "Line Position of Caret:" & tb.LineFromChar(tb.SelStart) & " "
Text 230, 72, "Character Position of Caret:" & tb.SelStart & " "
Text 230, 88, "Character No at Start of Caret Line:" & tb.CharFromLine(tl) & " "
Text 230, 104, "X Position of Caret:" & tb.ColFromChar(tb.SelStart) & " "
Text 230, 120, "Y Position of Caret:" & tb.RowFromChar(tb.SelStart) & " "
EndSub
This has been fixed in OCX versions greater than 2.36.[Reported by Roger Cabo, 22/05/2002]
{Created by Sjouke Hamstra; Last updated: 29/06/2022 by James Gaite}