Finddialog search Richtext

Please discuss general Delphi programming topics here.

Finddialog search Richtext

Postby Blod » May 15th, 2011, 7:00 pm

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;
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Finddialog search Richtext

Postby Kambiz » May 15th, 2011, 10:38 pm

In addition to focus the control, add the following line of code:

Code: Select all
keeprtf.Perform(EM_SCROLLCARET, 0, 0);
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Finddialog search Richtext

Postby Blod » May 16th, 2011, 7:44 am

Thanks,

That works.

Blod
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Finddialog search Richtext

Postby Blod » May 17th, 2011, 3:06 pm

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;
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Finddialog search Richtext

Postby Kambiz » May 17th, 2011, 5:56 pm

Did you try to scroll the caret before and after setting the background color to see the difference?
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Finddialog search Richtext

Postby Blod » May 17th, 2011, 6:19 pm

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
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Finddialog search Richtext

Postby Kambiz » May 18th, 2011, 10:53 pm

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);
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Finddialog search Richtext

Postby Blod » May 19th, 2011, 1:17 pm

Thanks for sorting things out.

Blod
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron