Solved

# move cursor in TRichEdit

Posted on 1999-07-22
Medium Priority
1,014 Views
How can we change the cursor position to another line by
the Delphi code?
0
Question by:mathematics

Author Comment

ID: 1390366
and how to move to the last line?
0

Accepted Solution

Vovbager earned 200 total points
ID: 1390367
SendMessage(RichEdit1.Handle,EM_SETSEL,20,20);

this example sets cursor in RichEdit at 20th char position.

0

LVL 7

Expert Comment

ID: 1390368
You can use these two procedures...

procedure TForm1.MoveToLine( ARichEdit : TRichEdit; LineNumber : Word);
var
Loopvariable : Word;
RunningLength : Word;
begin
RunningLength := 0;
for loopVariable := 0 to LineNumber-1 do
begin
RunningLength := RunningLength + Length(ARichEdit.Lines[LoopVariable]);
end;
SendMessage(ARichEdit.Handle, EM_SETSEL, RunningLength+1,RunningLength+1);
ARichEdit.Setfocus;
end;

procedure TForm1.MoveToLastLine(ARichEdit : TRichEdit);

var
Mylength : word;
LastLineLength : word;
SetPosition : word;
begin
MyLength := Length(ARichEdit.Lines.Text)-2;
LastLineLength := Length(ARichEdit.Lines[ARichEdit.Lines.Count-1]);
SetPosition := MyLength - LastLineLength;
SendMessage(ARichEdit.Handle, EM_SETSEL, SetPosition,SetPosition);
ARichEdit.Setfocus;
end;

Rob
0

LVL 27

Expert Comment

ID: 1390369
hi mathematics,

my version

procedure TForm1.Button1Click(Sender: TObject);
var
curLine,
firstCharNextLine : Integer;
begin
CurLine := Richedit1.Perform(EM_LINEFROMCHAR,Richedit1.Selstart,0);
firstCharNextLine := Richedit1.Perform(EM_LINEINDEX,CurLine+1,0);
If firstCharNextLine = -1 then
Showmessage('No Next Line Available')
else
Richedit1.SelStart := firstCharNextLine;
Richedit1.SetFocus;
end;

for the lastline use
firstCharNextLine := Richedit1.Perform(EM_LINEINDEX,Richedit1.lines.count-1,0);

meikl
0

LVL 7

Expert Comment

ID: 1390370
Meikl thats a much nicer solution :-)
0

Author Comment

ID: 1390371
thanks!
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.