Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

Caret position

I have some problems with positioning of caret to the beginning of the next line after the ending of text.

So i made this test app to resolve problem in another code where looks something is conflicting.

I have just two TRichedits and a TButton.
 On Buttonclick event am coping text from Richedi1 to Richedi2.
I need to have caret positioned at the beginning of the next line after the line where ending of text is.
procedure TForm1.Button1Click(Sender: TObject);
begin
   richedit2.text:=richedit1.text;
   richedit2.SelStart:=length(richedit2.Text);
   richedit2.SetFocus;
end;

end.

Open in new window

Avatar of pivar
pivar
Flag of Sweden image

Hi,

I think you need to add a newline, since the caret can't be positioned after the text.

/peter
I think that would be

richedit2.text := richedit1.text + #13 + #10;
richedit2.SelStart:=length(richedit2.Text);
Avatar of Ephraim Wangoya
try this
procedure TForm1.Button1Click(Sender: TObject);
var
  CaretPos: Integer;
begin
  richedit2.text:=richedit1.text;
  CaretPos := length(Trim(richedit2.Text)) - (Length(richedit2.lines[richedit2.Lines.Count -1]));
  richedit2.SelStart := CaretPos;
  richedit2.SetFocus;
end;

Open in new window

Sorry I misunderstood your question
procedure TForm1.Button1Click(Sender: TObject);
var
  CaretPos: Integer;
begin
  richedit2.text:=richedit1.text;
  CaretPos := length(Trim(richedit2.Text));
  if richedit2.lines[richedit2.Lines.Count -1] <> '' then
  begin
    richedit2.Lines.Add('');
    Inc(CaretPos, 2); //+eoln
  end;
  richedit2.SelStart := CaretPos;
  richedit2.SetFocus;
end;

Open in new window

Avatar of pr2501
pr2501

ASKER

Ewangoya
In test app it works fine.
But in working app is not.
Would you help me to ressolwe it?
Avatar of pr2501

ASKER

In my working code (not the test version from above) by clicking on button i am automatically inserting line of text. But no way to move caret to bagging of next line.

I must move it by typing on "enter".
So what code can simulate like enter was pressed?

Sorry took so long to reply. Had a very busy week.
This will work for all cases
procedure TForm1.Button1Click(Sender: TObject);
var
  CaretPos: Integer;
begin
  richedit2.text := Trim(richedit1.text);
  richedit2.Lines.Add('');
  CaretPos := length(richedit2.Text);
  richedit2.SelStart := CaretPos;
  richedit2.SetFocus;
end;

Open in new window

Avatar of pr2501

ASKER

No, still it does not work.

I am adding lines of text by clicking on the button using next:
 AddColorLine(RichEdit1, DateTimeToStr(Now)+'  '+' i ',form2.SelectedSb.Font.Color );
 

Way after typing of character "d" (attached picture) caret moves from position (see arrow) to the end of the line next time i press on button?
   
procedure tform2.AddColorLine (RichEdit : TRichEdit; Str : String; Color : TColor);
var
  pos_start      : Integer;
  pos_end ,line ,CaretPos      : Integer;
  old_SelAttr    : TTextAttributes;
begin
  // take the old SelAttributes
  old_SelAttr := RichEdit.SelAttributes;
  // get the end pos
  pos_start := length(RichEdit.Text);
  // add the text
  RichEdit.Lines.Add(Str);
  // the the new end position
  pos_end := length(RichEdit.Text);
  // colorize the text
  RichEdit.SelStart := pos_start;
  RichEdit.SelLength := pos_end - pos_start;
  RichEdit.SelAttributes.Color := Color;
  // put back the old attributes
  RichEdit.SelAttributes := old_SelAttr;

RichEdit1.SelStart := length(RichEdit1.Text);
RichEdit1.SetFocus;

  end;

Open in new window

caretpozition.JPG
here, add an empty line
procedure tform2.AddColorLine (RichEdit : TRichEdit; Str : String; Color : TColor);
var
  pos_start      : Integer;
  pos_end ,line ,CaretPos      : Integer;
  old_SelAttr    : TTextAttributes;
begin
  // take the old SelAttributes
  old_SelAttr := RichEdit.SelAttributes;
  // get the end pos
  pos_start := length(RichEdit.Text);
  // add the text
  RichEdit.Lines.Add(Str);
  // the the new end position
  pos_end := length(RichEdit.Text);
  // colorize the text
  RichEdit.SelStart := pos_start;
  RichEdit.SelLength := pos_end - pos_start;
  RichEdit.SelAttributes.Color := Color;
  // put back the old attributes
  RichEdit.SelAttributes := old_SelAttr;

  //Add an empty line then move to it
  RichEdit1.Add('');
  RichEdit1.SelStart := length(RichEdit.Text);
  RichEdit1.SetFocus;

  end;

Open in new window

Change to
procedure tform2.AddColorLine (RichEdit : TRichEdit; Str : String; Color : TColor);
var
  pos_start      : Integer;
  pos_end ,line ,CaretPos      : Integer;
  old_SelAttr    : TTextAttributes;
begin
  // take the old SelAttributes
  old_SelAttr := RichEdit.SelAttributes;
  // get the end pos
  pos_start := length(RichEdit.Text);
  // add the text
  RichEdit.Lines.Add(Str);
  // the the new end position
  pos_end := length(RichEdit.Text);
  // colorize the text
  RichEdit.SelStart := pos_start;
  RichEdit.SelLength := pos_end - pos_start;
  RichEdit.SelAttributes.Color := Color;
  // put back the old attributes
  RichEdit.SelAttributes := old_SelAttr;

  //Add an empty line then move to it
  RichEdit.Add('');
  RichEdit.SelStart := length(RichEdit.Text);
  RichEdit.SetFocus;

  end;

Open in new window

Avatar of pr2501

ASKER

Now after i click to add new line caret moves for 2 lines down.
caretpozition.2JPG.JPG
Avatar of pr2501

ASKER

I have  two situations:

1. from picture in previous post:
just clicking on a button many times. itch time caret is positioned 2 lines after text.
What is wrong.

2.picture in this post:
itch time i click on a button i add character also,
and in this situation is alright.
caretpozition3.JPG
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

I have used code below and no help the problem still insists.
Am sorry. I do not know what to say.

This is the last thing what must i resolve to complete this phase of project.
Thank you.
richedit1.SelLength := 0;
   richedit1.SelStart := Length( richedit1.Lines.Text);
  if  richedit1.CaretPos.X > 1 then
  begin
    richedit1.Lines.Append('');
    richedit1.SelLength := 0;
    richedit1.SelStart := Length( richedit1.Lines.Text);
  end;

Open in new window

Trim the added string, lets try this way
procedure tform2.AddColorLine (RichEdit : TRichEdit; Str : String; Color : TColor);
var
  pos_start      : Integer;
  pos_end ,line ,CaretPos      : Integer;
  old_SelAttr    : TTextAttributes;
  Temp: string;
begin
  Temp := Trim(Str);
  // take the old SelAttributes
  old_SelAttr := RichEdit.SelAttributes;
  // get the end pos
  pos_start := length(RichEdit.Text);
  // add the text
  RichEdit.Lines.Add(Temp);
  // the the new end position
  pos_end := length(RichEdit.Text);
  // colorize the text
  RichEdit.SelStart := pos_start;
  RichEdit.SelLength := pos_end - pos_start;
  RichEdit.SelAttributes.Color := Color;
  // put back the old attributes
  RichEdit.SelAttributes := old_SelAttr;

  //Add an empty line then move to it
  RichEdit.Add('');
  RichEdit.SelStart := length(RichEdit.Text);
  RichEdit.SetFocus;

  end;

Open in new window

Avatar of pr2501

ASKER

No it does not help.
I have took couple of days and maybe i will take some more to rest from this.

And then i will analyze the problem from the beginning.
It can't be so complicated. i just need to add line of text by an  event.
Maybe it is little more complicated because the text must be of special color, but...

Thank you
 
No problem
Avatar of pr2501

ASKER

If i use  (' ') at the end t works. And with ('') it will not.
(attached code). But in this case i have empty space on the beginning of the line, and i would rather do not have it.

Now maybe some of Yours code which you post it already can make it work, but can you find it out for me, pleas?
.
AddColorLine(RichEdit1,DateTimeToStr(Now)+'  '+ 'i',form2.SelectedSb.Font.Color );    

    richedit1.Perform(EM_SCROLLCARET,0,0);
    richedit1.SelStart := Length( richedit1.Lines.Text);
    richedit1.SetFocus;
    richedit1.Lines.Add(' ');

Open in new window

// original version
procedure tform2.AddColorLine (RichEdit : TRichEdit; Str : String; Color : TColor);
var
  pos_start      : Integer;
  pos_end ,line ,CaretPos      : Integer;
  old_SelAttr    : TTextAttributes;
  Temp: string;
begin
  // take the old SelAttributes
  old_SelAttr := RichEdit1.SelAttributes;
  // get the end pos
  pos_start := length(RichEdit1.Text);
  // add the text
  RichEdit1.Lines.Add(str);
  // the the new end position
  pos_end := length(RichEdit1.Text);
  // colorize the text
  RichEdit1.SelStart := pos_start;
  RichEdit1.SelLength := pos_end - pos_start;
  RichEdit1.SelAttributes.Color := Color;
  // put back the old attributes
  RichEdit1.SelAttributes := old_SelAttr;
  end;

Open in new window

Avatar of pr2501

ASKER

Maybe at the end (after all other code) i  should move caret for one position left (back)?
Avatar of pr2501

ASKER

   richedit1.Lines.Add('a');
    richedittext:=richedit1.Text;
    delete(richedittext, length(richedittext), 1);

Why i can not delete "a" with code above?
 
Avatar of pr2501

ASKER

And how many characters string variable can store.
To store a book what would i need?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

To remove that last character, you actually need to truncate the EOLN and Carriage Return characters as well

var
  richedittext: string;
begin
 richedit1.Lines.Add('a');
 richedittext:= TruncateEOLN(Trim(richedit1.Text));
 delete(richedittext, length(richedittext), 1);
 richedit1.Text  := richedittext;
function TruncateEOLN(const S: string): string;
var
  Len: Integer;
begin
  Result := S;
  Len := Length(Result);

  while (Result[Len] in [#13, #10]) do
    Dec(Len);
  if Len <> Length(Result) then
    SetLength(Result, Len);
end;

Open in new window

Avatar of pr2501

ASKER

Not good, because this function moves caret to the start of RE and i get text font of all RE green.
big mistake:
richedit.text := richedit.text

this will loose all formatting !!!!

i showed you how with streams
if you don't use the code with the stream copy, then your solution will never work.

richedit is not a good choice for big/large documents

Avatar of pr2501

ASKER

Thank you.
I will work on it after long wikend holiday.

Was just showing you how to delete the last character, this has nothing to do with the issue of moving the caret to the end of the memo. That is yet to be solved.
I'm still looking into it
Avatar of pr2501

ASKER

thank you

The Author is still waiting for my response, I'll work on this in the next few hours