Page 1 of 1

Finddialog search Richtext

PostPosted: May 15th, 2011, 7:00 pm
by Blod
Hi,

I've used the code below to search a memo and it has always worked as it should.
When I use it to search a rich text it works in as much as it finds all instances of the searched for text but doesn't programatically move the scrollbar down to show instances on the next page down as it should
do

Hope that I've made things clear

Thanks

Blod

Code: Select all
var Buffer, Pos, tPointer : PChar;
    BuffLength            : Word;
    line:integer;


begin
With Sender as TFindDialog do
   begin
      GetMem(tPointer, Length(FindText) + 1);
      StrPCopy(tPointer, FindText);
      BuffLength:= keeprtf.GetTextLen + 1;
      GetMem(Buffer,BuffLength);
      keeprtf.GetTextBuf(Buffer,BuffLength);
      Pos:= Buffer + keeprtf.SelStart + keeprtf.SelLength;
      Pos:= StrPos(Pos, tPointer);
      if Pos = NIL then MessageBeep(0)
      else
      begin
         keeprtf.SelStart:= Pos - Buffer;
         keeprtf.SelLength:= Length(FindText);
      end;
      FreeMem(tPointer, Length(FindText) + 1);
      FreeMem(Buffer,BuffLength);
      keeprtf.SetFocus; //This line highlight the found text
  end;
end;

Re: Finddialog search Richtext

PostPosted: May 15th, 2011, 10:38 pm
by Kambiz
In addition to focus the control, add the following line of code:

Code: Select all
keeprtf.Perform(EM_SCROLLCARET, 0, 0);

Re: Finddialog search Richtext

PostPosted: May 16th, 2011, 7:44 am
by Blod
Thanks,

That works.

Blod

Re: Finddialog search Richtext

PostPosted: May 17th, 2011, 3:06 pm
by Blod
Kambiz,

I spoke too soon when I said the problem was fixed.
When I tried it out in a simple test program it worked fine.
However, when I re-introduced it in my actual program I had the same trouble as

before.
By trial and error I've worked out what is causing the problem.
It is the procedure below which allows the user to colour the background of selected

text. As such it works fine but it stops the Scrollbar moving automatically during a

search of the richedit.

It doesn't seem to make sense I know but it is having the effect I mention.
Any thoughts?

Blod.

Code: Select all
procedure RE_SetSelBgColor(RichEdit: TRichEdit; AColor: TColor);
var
  Format: CHARFORMAT2;
begin
  FillChar(Format, SizeOf(Format), 0);
  with Format do
  begin
    cbSize := SizeOf(Format);
    dwMask := CFM_BACKCOLOR;
    crBackColor := AColor;
    Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint

(@Format));
  end;
end;

// Example: Set clYellow background color for the selected text.
procedure TForm1.Button1Click(Sender: TObject);
begin
  RE_SetSelBgColor(RichEdit1, clYellow);
end;

uses
  RichEdit;

Re: Finddialog search Richtext

PostPosted: May 17th, 2011, 5:56 pm
by Kambiz
Did you try to scroll the caret before and after setting the background color to see the difference?

Re: Finddialog search Richtext

PostPosted: May 17th, 2011, 6:19 pm
by Blod
Yes I did,
The scrolling worked before the background was set.
It didn't work after, even when the text that had been backgrounded was deleted.

Blod

Re: Finddialog search Richtext

PostPosted: May 18th, 2011, 10:53 pm
by Kambiz
The problem is not because of setting the background color of text. It's because of using RichEdit unit that re-defines EM_SCROLLCARET constant with the wrong message identifier!

To get rid of the problem, use the full qualified constant name Messages.EM_SCROLLCARET to specify the correct message identifier.

Code: Select all
keeprtf.Perform(Messages.EM_SCROLLCARET, 0, 0);

Re: Finddialog search Richtext

PostPosted: May 19th, 2011, 1:17 pm
by Blod
Thanks for sorting things out.

Blod