LineCount, LineFromChar, CharFromLine, RowFromChar, ColFromChar, GetLineFromChar Methods

Purpose

These TextBox and RichEdit control methods return information about positions in the text.

Syntax

% = Object.LineCount (or RichEdit.LineCnt)

% = Object.LineFromChar(index%)

% = Object.CharFromLine(index%)

% = Object.RowFromChar(index%)

% = Object.ColFromChar(index%)

% = Object.GetLineFromChar(index%)

Object: TextBox, RichEdit Ocx

Description

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.

Example

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

Known Issues

  1. In earlier versions, CharFromLine returned an erroneous value, falling behind one character for every line. GFABASIC uses the value derived from sending the EM_LINEINDEX message which counts all end of line markers as one character (in RTF the end of line marker is /par); however, the Text (as opposed to RTFText) uses CRLF (#13#10) as end of line markers which count as two characters: hence the discrepancy. The workaround for this is to add the line number to the result of CharFromLine.

    This has been fixed in OCX versions greater than 2.36.
    [Reported by Roger Cabo, 22/05/2002]

  2. When using LineFromChar and RowFromChar, if the cursor or caret is placed at the position following the last text character, -1 is returned. This is the default behaviour of the underlying Windows message so could be counted as proper behaviour; there are no workarounds and this will not be fixed unless the underlying Windows message call is fixed.
    [Reported by James Gaite, 09/06/2022]
  3. When using LineFromChar the returned x-coordinate seems not to go above a value between 180 and 190, no matter how many lines there are in the control. This is the default behaviour of the underlying Windows message so could be counted as proper behaviour although it obviously is not; there are no workarounds and this will not be fixed unless the underlying Windows message call is fixed.
    [Reported by James Gaite, 09/06/2022]

See Also

TextBox, RichEdit

{Created by Sjouke Hamstra; Last updated: 29/06/2022 by James Gaite}