help replacing a character

General TRichView support forum. Please post your questions here
Post Reply
KFC123
Posts: 13
Joined: Fri Apr 21, 2006 7:27 am

help replacing a character

Post by KFC123 »

Hi there,
I am writing a copybook-like editor with richview

// First of all, I build two text style: one for background text in gray and
// one for normal text (typing) in green

// then, I add the background text which apply the textstyle(0)
CopyBook.InsertText('This is a test text', true);

// then, I rewrite the keypress function where I fill each character
// with a specific color
procedure TTestForm.CopyBookKeyPress(Sender: TObject; var Key: Char);
var
ss :Integer;
ch :String;
begin
if (key<>#13) and (key>#30) then
begin
CopyBook.ApplyTextStyle(1); // textstyle(1) for typing character
CopyBook.InsertText(key, False);
key := #0;
end;
end;

Now, everything is all right expect that the charater where I type will be inserted to the current caret position rather than replace the character at current position. Any approach for that problem.

BTW, how can I capture when the Enter is pressed?

Thanks in advance.
Sergey Tkachenko
Site Admin
Posts: 17562
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

KFC123
Posts: 13
Joined: Fri Apr 21, 2006 7:27 am

Post by KFC123 »

Thanks, it works. However, I found it is very flickering while typing. Here is my code

procedure TTestForm.SEKeyPress(Sender: TObject; var Key: Char);
var
aword :string;
rve: TCustomRichViewEdit;
ItemNo, Offs: Integer;
begin

if key<>#13 then
begin
if (key>#30) then
begin
try
sent := string(background_text_in_gray[curPos]);

// if what you type exactly matches the text in background
if (sent=key) then
begin
Inc(curPos);
rve := SentenceEdit.TopLevelEditor;
if rve.SelectionExists then
exit;
ItemNo := rve.CurItemNo;
Offs := rve.OffsetInCurItem;
if (Offs>=rve.GetOffsAfterItem(ItemNo)) then
begin
if (ItemNo+1<rve.ItemCount) and not rve.IsFromNewLine(ItemNo+1) then
begin
inc(ItemNo);
Offs := rve.GetOffsBeforeItem(ItemNo);
end
else exit;
end;
rve.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs+1);
rve.ApplyTextStyle(1); // show the typed character in green
rve.InsertText(key, False); // in order to control the style
rve.Invalidate;
end;
except
on E:Exception do begin {exception shown here} end;
end;
key := #0;
end
end;
end;

Any idea?
Sergey Tkachenko
Site Admin
Posts: 17562
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Try removing call of rve.Invalidate. It's not needed, and usually it's not required to redraw the whole window.
KFC123
Posts: 13
Joined: Fri Apr 21, 2006 7:27 am

Post by KFC123 »

Sergey Tkachenko wrote:Try removing call of rve.Invalidate. It's not needed, and usually it's not required to redraw the whole window.
Thanks for your reply. Yes, it is little better after removing the invlidate, however, the problem still no good. When I type, the text after the current character is shakeing seriously.
Sergey Tkachenko
Site Admin
Posts: 17562
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

One more suggestion: change ApplyTextStyle(1) to CurTextStyleNo := 1;
KFC123
Posts: 13
Joined: Fri Apr 21, 2006 7:27 am

Post by KFC123 »

Sergey Tkachenko wrote:One more suggestion: change ApplyTextStyle(1) to CurTextStyleNo := 1;
Yes, it does cure the problem a little bit more.

I try to use TRichEdit instead, the problem even worse. However, when I type in TRichedit, only the text on the same line of the current charater is flickering. While I use TRichview, only the text after the current charater is shakeing, rather than flickering. !?
Sergey Tkachenko
Site Admin
Posts: 17562
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

There are two operations here, deleting selection and inserting. We removed one unnecessary operation (applying style to the selection), but these 2 are necessary.
If the window is updated after deletion, the text after the insertion point may shake a bit.

Well, you can forbid redrawing while making this operation, but in this case you'll need to redraw the whole window after. There will be no flickering, but typing will be slower (usually TRichView does not redraw the whole window on editing operations)
Post Reply