Hello All,
I have tried to make a search form i Delphi XE8 with TWpRichText.Finder - but I cant get it to work. It seems that it jumps over some instances of found text in the TWPRicText document! I have tried several times to get it to work - but I can't figure it out :-( Can you help me?
My aim is to make a searchform that can find and select every instances of the searchtext - one at a time. But how to do this?
Thanks in advance.
Michael
Code
- unit fmSearchWpU;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ExtCtrls, System.Actions, Vcl.ActnList,
- WPRTEDefs, WPCTRMemo, WPCTRRich, stdProc;
- type
- TfmSearchWp = class(TForm)
- emcxLabel1: TLabel;
- teTextToFind: TEdit;
- rgSearchDirection: TRadioGroup;
- emcxButton1: TButton;
- ActionList1: TActionList;
- actSearch: TAction;
- procedure actSearchExecute(Sender: TObject);
- private
- { Private declarations }
- FWp: TWPRichText;
- public
- { Public declarations }
- class procedure ShowSearchWp(AWPRichText: TWPRichText; ACaption: String);
- end;
- var
- fmSearchWp: TfmSearchWp;
- implementation
- {$R *.dfm}
- { TfmSearchWp }
- procedure TfmSearchWp.actSearchExecute(Sender: TObject);
- begin
- with FWp.Finder do begin
- // if rgSearchDirection.ItemIndex = 0 then
- // ToEnd
- // else
- // ToStart;
- while true do begin
- if rgSearchDirection.ItemIndex = 0 then begin
- if not prev(teTextToFind.Text) then break;
- SelectText;
- FWp.ScrollToPosition(FWp.CPPosition, 0, 10);
- end else begin
- if not Next(teTextToFind.Text) then break;
- SelectText;
- FWp.ScrollToPosition(FWp.CPPosition, 0, 10);
- end;
- end;
- end;
- end;
- class procedure TfmSearchWp.ShowSearchWp(AWPRichText: TWPRichText;
- ACaption: String);
- begin
- Application.CreateForm(TfmSearchWp, fmSearchWp);
- try
- fmSearchWp.FWp := AWPRichText;
- fmSearchWp.FWp.CPPosition := 0;
- fmSearchWp.Caption := ACaption;
- fmSearchWp.ShowModal;
- finally
- FreeAndNil(fmSearchWp);
- end;
- end;
- end.