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

pr2501Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Geert GConnect With a Mentor Oracle dbaCommented:
works with this:
procedure TForm1.CopyRT(re1, re2: TRichEdit);
var m: TStream;
begin
  m := TMemoryStream.Create;
  try
    re1.Lines.SaveToStream(m);
    m.Seek(0, soFromBeginning);
    re2.Lines.LoadFromStream(m);
  finally
    m.Free;
  end;
end;

procedure TForm1.MoveRTCaretEnd(re: TRichEdit);
begin
  re.SelLength := 0;
  re.SelStart := Length(re.Lines.Text);
  if re.CaretPos.X > 1 then
  begin
    re.Lines.Append('');
    re.SelLength := 0;
    re.SelStart := Length(re.Lines.Text);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyRT(RichEdit1, RichEdit2);
  MoveRTCaretEnd(RichEdit2);
end;

Open in new window

0
 
pivarCommented:
Hi,

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

/peter
0
 
pivarCommented:
I think that would be

richedit2.text := richedit1.text + #13 + #10;
richedit2.SelStart:=length(richedit2.Text);
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ephraim WangoyaCommented:
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

0
 
Ephraim WangoyaCommented:
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

0
 
pr2501Author Commented:
Ewangoya
In test app it works fine.
But in working app is not.
Would you help me to ressolwe it?
0
 
pr2501Author Commented:
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?
0
 
Ephraim WangoyaCommented:

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

0
 
pr2501Author Commented:
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
0
 
Ephraim WangoyaCommented:
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

0
 
Ephraim WangoyaCommented:
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

0
 
pr2501Author Commented:
Now after i click to add new line caret moves for 2 lines down.
caretpozition.2JPG.JPG
0
 
pr2501Author Commented:
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
0
 
pr2501Author Commented:
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

0
 
Ephraim WangoyaCommented:
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

0
 
pr2501Author Commented:
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
 
0
 
Ephraim WangoyaCommented:
No problem
0
 
pr2501Author Commented:
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

0
 
pr2501Author Commented:
Maybe at the end (after all other code) i  should move caret for one position left (back)?
0
 
pr2501Author Commented:
   richedit1.Lines.Add('a');
    richedittext:=richedit1.Text;
    delete(richedittext, length(richedittext), 1);

Why i can not delete "a" with code above?
 
0
 
pr2501Author Commented:
And how many characters string variable can store.
To store a book what would i need?
0
 
Ephraim WangoyaConnect With a Mentor Commented:

To delete
    richedit1.Lines.Add('a');
    //trim any extra characters from the text  
    richedittext := Trim(richedit1.Text);
    delete(richedittext, length(richedittext), 1);

You should not store a book in the database as a string of characters, that is not efficient for the database.
You need to use a binary format, you can store it as PDF or Word Document;

If you need to store a large number of books, you should consider storing the files on disk and having a field reference the location of the file. Large files in the database are not good for performance and also your database will grow very large very quickly.

Now back to that caret positioning, let me see if I can come up with any other idea
0
 
Ephraim WangoyaCommented:

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

0
 
pr2501Author Commented:
Not good, because this function moves caret to the start of RE and i get text font of all RE green.
0
 
Geert GOracle dbaCommented:
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

0
 
pr2501Author Commented:
Thank you.
I will work on it after long wikend holiday.
0
 
Ephraim WangoyaCommented:

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
0
 
pr2501Author Commented:
thank you
0
 
Ephraim WangoyaCommented:

The Author is still waiting for my response, I'll work on this in the next few hours
0
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.

All Courses

From novice to tech pro — start learning today.