I'm trying to write a function that returns the line and column number the cursor is on in a richedit/memo component. As far as I know, there isn't any fields that keep a record of this.
However, my function is designed to be called repeatedly and as it stands it's far too slow. Can anyone think of a faster algorithm?
procedure TFindReplace.GetLineNumber( Position: Integer; var LineNumber, ColumnNumber: integer );
{this procedure must be very efficient if it is to be called continuosly.
At the moment it is not that efficient}
var
i: integer;
indexOfBeginningOfLastLine: integer;
begin
{initialise line number to 0}
LineNumber := 0;
indexOfBeginningOfLastLine := 0;
{increment line number each time we encounter a newline (#$D) in the text}
for i := 1 to Position do
if fEditor.Text = #$D then
begin
inc( LineNumber );
indexOfBeginningOfLastLine := i;
end;
{get the columnNumber}
ColumnNumber := Position - (indexOfBeginningOfLastLine+1);
end;
However, my function is designed to be called repeatedly and as it stands it's far too slow. Can anyone think of a faster algorithm?
procedure TFindReplace.GetLineNumber( Position: Integer; var LineNumber, ColumnNumber: integer );
{this procedure must be very efficient if it is to be called continuosly.
At the moment it is not that efficient}
var
i: integer;
indexOfBeginningOfLastLine: integer;
begin
{initialise line number to 0}
LineNumber := 0;
indexOfBeginningOfLastLine := 0;
{increment line number each time we encounter a newline (#$D) in the text}
for i := 1 to Position do
if fEditor.Text = #$D then
begin
inc( LineNumber );
indexOfBeginningOfLastLine := i;
end;
{get the columnNumber}
ColumnNumber := Position - (indexOfBeginningOfLastLine+1);
end;