Recent

Author Topic: Cursor position in a memo  (Read 30631 times)

w click

  • Full Member
  • ***
  • Posts: 183
Cursor position in a memo
« on: November 11, 2011, 10:54:07 am »
I want to set the cursor position in a memo to a particular line, but I can't find anything that works.  Can anyone help please.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Cursor position in a memo
« Reply #1 on: November 11, 2011, 11:02:07 am »
You must use TMemo.CaretPos.

Ooops.  Maybe not.  It seems to be only to get the Caret location.  I will look further.

I think you must use TMemo.SelStart.

« Last Edit: November 11, 2011, 11:18:32 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #2 on: November 11, 2011, 12:46:04 pm »
To go to newline:-

Quote
  memo1.selstart:=pos(memo1.lines[newline],memo1.text)-1;
  Memo1.SelLength:=0;
  Memo1.Perform(EM_SCROLLCARET, 0, 0);
  Memo1.SetFocus;

This works, but it's out by a few characters.  The cursor appears half a dozen characters from the start.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Cursor position in a memo
« Reply #3 on: November 11, 2011, 01:04:49 pm »
wclick, you got there before me :)  I think this will work, but my test was only very quick.

Add LCLProc to the uses statement

uses LCLProc

  Memo1.selstart:= UTF8Pos('Found it!',Memo1.text)-1;  //UTF8Pos seems to work better
  Memo1.SelLength:=0;
  Memo1.SetFocus;
Lazarus Trunk / fpc 2.6.2 / Win32

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #4 on: November 11, 2011, 01:22:16 pm »
Oh, thanks, that's really rather satisfying.  Works great.

I set SelLength to the length of memo1.lines[newline]; so it jumps very nicely to the right place and hightlights the line in question as the cursor can be tricky to spot sometimes.

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #5 on: November 11, 2011, 03:47:10 pm »
Quote
Memo1.selstart:= UTF8Pos('Found it!',Memo1.text)-1;  //UTF8Pos seems to work better
This is great and works for the first instance.  How do I get to the next example of 'Found it!'?

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #6 on: November 11, 2011, 06:03:26 pm »
Arrghh, this doesn't work!!!
If you have two lines in the memo that are the same, then you cannot jump to the second example.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Cursor position in a memo
« Reply #7 on: November 11, 2011, 06:19:05 pm »
Try something like this:

Code: [Select]
uses strutils;
var lastPos: integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  lastPos := Pos('text to find', Memo1.Text);
  Memo1.SelStart := lastPos - 1;
  Memo1.SelLength:=0;
  Memo1.Perform(EM_SCROLLCARET, 0, 0);
  Memo1.SetFocus;
end;

procedure TForm1.Button2Click(Sender: TObject);
var newPos: integer;
begin
  newPos := PosEx('text to find', Memo1.Text, lastPos + 1);
  Memo1.SelStart := newPos - 1;
  Memo1.SelLength:=0;
  Memo1.Perform(EM_SCROLLCARET, 0, 0);
  Memo1.SetFocus;
end;               

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #8 on: November 11, 2011, 06:48:52 pm »
I cobbled together something from that which works, but veers off as you go down the memo.

I need a UTG8PosEx function.

There's AnsiPosEx, but all the information I can find is in Japanese.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Cursor position in a memo
« Reply #9 on: November 11, 2011, 07:29:10 pm »
store SelStart in a variable
move SelStart to the end of whatever your target was
Set SelLength:= Length(Memo1.Text)
copy SelText to a temporay string
get the UTF8Pos of the Taget in the temporary string
Add that to the stored SelStart
reset SelStart to the new value
Lazarus Trunk / fpc 2.6.2 / Win32

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #10 on: November 12, 2011, 09:39:28 am »
Yes, that would work, thanks.

I was hoping for something really straight forward.  I find it strange that some things that seem like they'll be obvious before you start, aren't.  Like moving around in a memo.  I've already done a few seemingly much more complicated things in this project without trouble.  That's just a comment really.

w click

  • Full Member
  • ***
  • Posts: 183
Re: Cursor position in a memo
« Reply #11 on: November 14, 2011, 10:35:39 am »
This works.  The UTF8Copy is quite important.  Thanks for all the help.

Quote
procedure TForm1.ButtonSearchClick(Sender: TObject);
var oldpos,newpos : integer;
    newtext       : string;
begin
  memo1.selstart:=memo1.selstart+1;
  memo1.sellength:=0;
  oldpos:=memo1.selstart;
  newtext:=UTF8copy(memo1.text,memo1.selstart+1,length(memo1.text));
  newpos:=UTF8Pos(edit1.text,newtext)-1;
  memo1.selstart:=oldpos+newpos;
  memo1.sellength:=length(edit1.text);
  Memo1.Perform(EM_SCROLLCARET, 0, 0);
  Memo1.SetFocus;
end;

What I can do

  • Full Member
  • ***
  • Posts: 167
Re: Cursor position in a memo
« Reply #12 on: April 29, 2024, 08:17:52 pm »
food for thought
I ran into the same situation years ago so I made a brute force work around, that worked well when ever I needed line info I would switch to zap the memo to a StringList do my search of line number info that would then match my memo. I would just bounce between the tow and get what ever info I wanted but I didn't have any of the cool stuff that exist in todays components.

 

TinyPortal © 2005-2018