Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with function

Status
Not open for further replies.

bearsite4

Programmer
Sep 29, 2001
79
AU
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;
 
A RichEdit has a Lines property, like a memo. Can you just loop through the lines, adding the length of each until you are about to go over your position, then you've got the line you're on and the remainder is the column?

Just an idea... I'm not sure if it will work with richedit tags.

Good luck
TealWren
 
Hi Bearsite4

Have another look at my post in thread 102-174312,
this use an API function to read the cursor position from a rich edit.

Hope it might be of some help.
Steve
 
How do I access that thread? I've tried the search but it doesn't really help.
 
Hi Bearsite4

The Post is 'Scroll up/down to selected text'.
posted by Bobbafet on the 20th Oct.
sorry about the thread number thisng . I have seen others refer to posts this way but not tried it myself!!.

Steve..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top