• 程序中的对话框应用(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;

     

  • 相关阅读:
    第十六届全国大学智能汽车竞赛竞速比赛规则-讨论稿
    从0到1设计一台8bit计算机
    在 CentOS7 上安装 MongoDB
    sea.js五分钟上手
    自动调试自动编译五分钟上手
    自动调试自动编译五分钟上手
    在react底下安装环境
    在react底下安装环境
    推荐几款好用的云笔记软件
    推荐几款好用的云笔记软件
  • 原文地址:https://www.cnblogs.com/fansizhe/p/12784190.html
Copyright © 2020-2023  润新知