回 帖 发 新 帖 刷新版面

主题:finddialog怎样使用?

大家好! 我是一个初学者 我希望大家多多教我
我想编一段程序,实现以下功能
form1上放了一个memo组件,我想MEMO1里面的内容进行查找,所以再放了一个finddialog
但是怎样进行查找,具体语句怎么写?
还望各位高手大侠指教,在下多谢了

回复列表 (共1个回复)

沙发

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    FindDialog1: TFindDialog;
    ReplaceDialog1: TReplaceDialog;
    Button2: TButton;
    procedure FindDialog1Find(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FindDialog1Find(Sender: TObject);//查找
  var
  FoundPos,InitPos:Integer;
begin
  InitPos:=Memo1.SelStart+Memo1.SelLength;
  FoundPos:=Pos(FindDialog1.FindText,Copy(Memo1.Text,InitPos+1,Length(Memo1.Text)-InitPos));
  if FoundPos>0 then
  begin
    Memo1.SetFocus;
    Memo1.SelStart:=InitPos+FoundPos-1;
    Memo1.SelLength:=Length(FindDialog1.FindText);
  end
  else
  begin
    MessageDlg('找不到“'+FindDialog1.FindText+'”',mtInformation,[mbOK],0);
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
with FindDialog1 do
  begin
    OnFind:=FindDialog1Find;
    Execute;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Memo1.SelStart:=0;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);//替换
var
  FoundPos,InitPos:Integer;
begin
  InitPos:=Memo1.SelStart+Memo1.SelLength;
  FoundPos:=Pos(ReplaceDialog1.FindText,Copy(Memo1.Text,InitPos+1,Length(Memo1.Text)-InitPos));
  if FoundPos>0 then
    begin
    Memo1.SetFocus;
    Memo1.SelStart:=InitPos+FoundPos-1;
    Memo1.SelLength:=Length(ReplaceDialog1.FindText);
    end
    else
    begin
    MessageDlg('找不到“'+ReplaceDialog1.FindText+'”',mtInformation,[mbOK],0);
    end;

end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
Memo1.SelText:=ReplaceDialog1.ReplaceText;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 with ReplaceDialog1 do
  begin
    OnFind:=ReplaceDialog1Find;
    Execute;
  end;

end;

end.

我来回复

您尚未登录,请登录后再回复。点此登录或注册