Difference between revisions of "TFindDialog"

From Free Pascal wiki
Jump to navigationJump to search
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''TFindDialog''' [[Image:tfinddialog.png]] is a component that aids in finding information. It can be found on the [[Dialogs tab]] of the [[Component Palette]].
+
{{TFindDialog}}
  
To activate a FindDalilog, just call its <tt>Execute()</tt> function. Create an <tt>OnFind</tt> method to perform the actual find.
+
'''TFindDialog''' [[Image:tfinddialog.png]] is a component that aids in searching information. It can be found on the [[Dialogs tab]] of the [[Component Palette]].
 +
 
 +
It is important to know that the dialog itself does not perform a search; it just provides a user interface for all the search parameters. To activate a FindDialog, call its <tt>Execute()</tt> [[Function|function]]. Create an <tt>OnFind</tt> [[Method|method]] in which the search itself is performed.
  
 
[[image:component-TFindDialog.png]]
 
[[image:component-TFindDialog.png]]
  
 
== Usage ==
 
== Usage ==
<syntaxhighlight>
+
The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo ([[TMemo]]).
procedure TMyForm.Button1Click(Sender: TObject);
+
<syntaxhighlight lang="pas">
 +
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
  FindDialog1.FindText := 'findme';
 
 
   FindDialog1.Execute();
 
   FindDialog1.Execute();
 
end;
 
end;
  
procedure TMyForm.FindDialog1Find(Sender: TObject);
+
procedure TForm1.FindDialog1Find(Sender: TObject);
 
var
 
var
   src: String;
+
   k: integer;
 
begin
 
begin
 
   with Sender as TFindDialog do begin
 
   with Sender as TFindDialog do begin
     src := ReplaceDialog1.FindText;
+
     k := Pos(FindText, Memo1.Lines.Text);
     k := Pos( src, Memo1.Lines.Text );
+
    if k > 0 then begin
     if k>0 then begin
+
      Memo1.SelStart := k - 1;
       Memo1.Selstart := k;
+
      Memo1.SelLength := Length(FindText);
       Memo1.SelLength := length( src );
+
      Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
 +
     end else
 +
      Beep();
 +
  end;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
If you want to repeat the operation several times with the same search text you must remember the last found position and continue searching from here - the function <tt>PosEx()</tt> in [[Unit|unit]] StrUtils is perfect for this purpose:
 +
 
 +
<syntaxhighlight lang="pas">
 +
uses
 +
  StrUtils;
 +
 
 +
type
 +
  TForm1 = class(TForm)
 +
  private
 +
    FFoundPos: Integer;
 +
    ...
 +
 
 +
procedure TForm1.Button1Click(Sender: TObject);
 +
begin
 +
  with FindDialog1 do
 +
  begin
 +
    if frEntireScope in Options then    // Search begins at file start
 +
      FFoundPos := 0
 +
    else
 +
      FFoundPos := Memo1.SelStart;      // Serach begins at current cursor position
 +
    Execute;
 +
  end;
 +
end;
 +
 
 +
procedure TForm1.FindDialog1Find(Sender: TObject);
 +
begin
 +
  with Sender as TFindDialog do
 +
  begin
 +
    FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1);
 +
     if FFoundPos > 0 then  
 +
    begin
 +
       Memo1.SelStart := FFoundPos - 1;
 +
       Memo1.SelLength := Length(FindText);
 +
      Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
 
     end else
 
     end else
 
       Beep();
 
       Beep();
Line 31: Line 73:
 
== See also ==
 
== See also ==
 
* [[doc:lcl/dialogs/tfinddialog.html|TFindDialog doc]]
 
* [[doc:lcl/dialogs/tfinddialog.html|TFindDialog doc]]
 +
* [[TReplaceDialog]]
  
 
{{LCL Components}}
 
{{LCL Components}}

Latest revision as of 08:38, 1 May 2020

English (en) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

TFindDialog tfinddialog.png is a component that aids in searching information. It can be found on the Dialogs tab of the Component Palette.

It is important to know that the dialog itself does not perform a search; it just provides a user interface for all the search parameters. To activate a FindDialog, call its Execute() function. Create an OnFind method in which the search itself is performed.

component-TFindDialog.png

Usage

The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo (TMemo).

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

procedure TForm1.FindDialog1Find(Sender: TObject);
var
  k: integer;
begin
  with Sender as TFindDialog do begin
    k := Pos(FindText, Memo1.Lines.Text);
    if k > 0 then begin
      Memo1.SelStart := k - 1;
      Memo1.SelLength := Length(FindText);
      Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
    end else
      Beep();
  end;
end;

If you want to repeat the operation several times with the same search text you must remember the last found position and continue searching from here - the function PosEx() in unit StrUtils is perfect for this purpose:

uses
  StrUtils;

type
  TForm1 = class(TForm)
  private
    FFoundPos: Integer;
    ...

procedure TForm1.Button1Click(Sender: TObject);
begin
  with FindDialog1 do 
  begin
    if frEntireScope in Options then    // Search begins at file start
      FFoundPos := 0
    else
      FFoundPos := Memo1.SelStart;      // Serach begins at current cursor position
    Execute;
  end;
end; 

procedure TForm1.FindDialog1Find(Sender: TObject);
begin
  with Sender as TFindDialog do 
  begin
    FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1);
    if FFoundPos > 0 then 
    begin
      Memo1.SelStart := FFoundPos - 1;
      Memo1.SelLength := Length(FindText);
      Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
    end else
      Beep();
  end;
end;

See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu