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.
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.
I think that would be
richedit2.text := richedit1.text + #13 + #10;
richedit2.SelStart:=length (richedit2 .Text);
richedit2.text := richedit1.text + #13 + #10;
richedit2.SelStart:=length
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;
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;
ASKER
Ewangoya
In test app it works fine.
But in working app is not.
Would you help me to ressolwe it?
In test app it works fine.
But in working app is not.
Would you help me to ressolwe it?
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?
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;
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.Co lor );
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?
I am adding lines of text by clicking on the button using next:
AddColorLine(RichEdit1, DateTimeToStr(Now)+' '+' i ',form2.SelectedSb.Font.Co
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;
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;
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;
ASKER
Now after i click to add new line caret moves for 2 lines down.
caretpozition.2JPG.JPG
caretpozition.2JPG.JPG
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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;
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;
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
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
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?
.
(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(' ');
// 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;
ASKER
Maybe at the end (after all other code) i should move caret for one position left (back)?
ASKER
richedit1.Lines.Add('a');
richedittext:=richedit1.Te xt;
delete(richedittext, length(richedittext), 1);
Why i can not delete "a" with code above?
richedittext:=richedit1.Te
delete(richedittext, length(richedittext), 1);
Why i can not delete "a" with code above?
ASKER
And how many characters string variable can store.
To store a book what would i need?
To store a book what would i need?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(richedit
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;
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
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
ASKER
Thank you.
I will work on it after long wikend holiday.
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
ASKER
thank you
The Author is still waiting for my response, I'll work on this in the next few hours
I think you need to add a newline, since the caret can't be positioned after the text.
/peter