zoukankan      html  css  js  c++  java
  • 程序中的对话框应用(6)- “替换”对话框

    TReplaceDialog组件是TFindDialog类的特殊版本,提示用户查找和替换字符串,与“查找”对话框相同,替换对话框是一个无模式的windows对话框。

    代码示例(窗体上添加按钮、RichEdit、ReplaceDialog):

    procedure TForm1.Button1Click(Sender: TObject);//打开替换对话框
    begin
      ReplaceDialog1.Top:= 200;
      ReplaceDialog1.Left:= 400;
      ReplaceDialog1.Execute;
    end;
    
    procedure TForm1.ReplaceDialog1Find(Sender: TObject);//在文本中查找要替换的内容
    var
      FoundAt: LongInt;
      StartPos,Toend: integer;
    begin
      with RichEdit1 do
      begin
        if SelLength<> 0 then
        StartPos:= SelStart+ SelLength
        else
          StartPos:= 0;
        Toend:= Length(Text)- StartPos;
        FoundAt:= FindText(ReplaceDialog1.FindText,StartPos,Toend,[stMatchCase]);
        if FoundAt <> -1 then
        begin
          SetFocus;
          SelStart:= FoundAt;
          SelLength:= Length(ReplaceDialog1.FindText);
        end;
      end;
    end;
    
    procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
    var
      SelPos: Integer;
    begin
      //对文本进行单个替换
      if frReplace in ReplaceDialog1.Options then
      begin
        with TReplaceDialog(Sender) do
        begin
          SelPos:= pos(FindText,RichEdit1.Lines.Text);
          if SelPos> 0 then
          begin
            RichEdit1.SelStart:= SelPos- 1;
            RichEdit1.SelLength:= Length(FindText);
            RichEdit1.SelText:= ReplaceText;
          end
          else
            MessageDlg(Concat('Could not find',Findtext,'in Memo1.'),mtError,[mbOK],0);
        end;
        //对文本进行全部替换
        if frReplaceAll in ReplaceDialog1.Options then
        while True do
        begin
          with TReplaceDialog(Sender) do
          begin
            if SelPos= 0 then
            Break;
          SelPos:= Pos(FindText,RichEdit1.Lines.Text);
            if SelPos> 0 then
            begin
              RichEdit1.SelStart:= SelPos- 1;
              RichEdit1.SelLength:= Length(FindText);
              RichEdit1.SelText:= ReplaceText;
            end;
          end;
        end;
      end;
    end;

     

  • 相关阅读:
    字符串的不可变性--转载
    this的作用--转载
    构造函数
    根基决定一个程序员会不会被淘汰 --转载
    BAT-使用BAT方法清理Delphi临时文件
    键盘,鼠标,文件
    画布.画笔.画刷
    Delphi外挂开发网站
    教程-经典Delphi教程网
    教程-Delphi各版本与工具下载地址
  • 原文地址:https://www.cnblogs.com/fansizhe/p/12784190.html
Copyright © 2011-2022 走看看