[Lazarus] No halp entry for FindDialog ...

Howard Page-Clark hdpc at talktalk.net
Sat Nov 8 02:57:42 CET 2014


On 08/11/2014 00:35, Bob B. wrote:
> and my Delphi 3 code for OnFind does not work in Lazarus, at least not
> in Laz 1.2.4 for Win32.  Could some kind person please offer an example
> that works?

Drop a TFindDialog, TMemo, TPopupMenu on the main form of a new default 
Lazarus project, and code unit1.pas thus:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
   Forms, Controls, Dialogs, StdCtrls, Menus;

type

   TForm1 = class(TForm)
     FindDialog1: TFindDialog;
     MTextToSearch: TMemo;
     PopupMenu1: TPopupMenu;
     procedure FormCreate(Sender: TObject);
   private
     procedure DoSearch(Sender: TObject);
     procedure DoSearchFromPopup(Sender: TObject);
     function Located(const aText: string): boolean;
   end;

var
   Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
   mi: TMenuItem;
begin
   mi:=TMenuItem.Create(PopupMenu1);
   mi.Caption:='Search unit1.pas';
   mi.OnClick:=@DoSearchFromPopup;
   PopupMenu1.Items.Add(mi);
   MTextToSearch.PopupMenu:=PopupMenu1;

   FindDialog1.Title:='Find text in unit1.pas';
   FindDialog1.OnFind:=@DoSearch;

   MTextToSearch.Align:=alClient;
   MTextToSearch.ScrollBars:=ssAutoBoth;
   MTextToSearch.Lines.LoadFromFile('unit1.pas');
   FindDialog1.Execute;
end;

procedure TForm1.DoSearch(Sender: TObject);
begin
   FindDialog1.CloseDialog;
   if not Located(FindDialog1.FindText) then
     ShowMessageFmt('Text "%s" not found in 
unit1.pas',[FindDialog1.FindText]);
end;

procedure TForm1.DoSearchFromPopup(Sender: TObject);
begin
   FindDialog1.Execute;
end;

function TForm1.Located(const aText: string): boolean;
var
   p: integer;
begin
   p:=Pos(aText, MTextToSearch.Text);
   Result:=p<>0;
   if Result then begin
     MTextToSearch.SelStart:=p-1;
     MTextToSearch.SelLength:=Length(aText);
   end;
end;

end.






More information about the Lazarus mailing list