Search text in a TMemo with FindDialog  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 25/12/99

Tip accessed 61 times

 


In this tip, we will use a FindDialog to look for text in a TMemo, higlighting the text when we find it.

  • Put a TMemo (Memo1), a FindDialog (FD1) and a TButton (Button1) in your form
  • Put this code in the OnClick event of Button1:


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



  • And in the OnFind event od FD1, put this other code:


     procedure TForm1.FD1Find(Sender: TObject);
     var
        sTemp:string;
        itemp:integer;
     begin
       sTemp:=Copy(Memo1.Text,
                   2+Memo1.SelStart,
                   Length(Memo1.Text)-Memo1.SelStart);
    
       iTemp:=Pos(UpperCase(FD1.FindText),UpperCase(sTemp));
    
       if iTemp<>0 then
       begin
         Memo1.SetFocus;
         Memo1.SelStart:=Memo1.SelStart+iTemp;
         Memo1.SelLength:=Length(FD1.FindText);
       end
         else
           {If don't find the searched text...}
           ShowMessage('No encuentro '+FD1.FindText);
     end;