Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Easy FindDialog question

Posted on 2000-04-04
2
367 Views
Last Modified: 2010-04-04
I've got a TFindDialog and a TReplaceDialog control. Both have an OnFind event on which I'm trying to use the following code:

procedure TfrmPrincipal.FindDialogFind(Sender: TObject);
var
  VentanaHija: TfrmVentanaHija; FoundAt: LongInt;
  StartPos, ToEnd, z: integer; TipoBusqueda: TSearchTypes;
  xText: string;
begin
  VentanaHija := ActiveMDIChild as TfrmVentanaHija;
  FoundAt := -1;

 {Determinar el tipo de búsqueda}
  if frWholeWord in FindDialog.Options then
    TipoBusqueda := TipoBusqueda + [stWholeWord];
  if frMatchCase in FindDialog.Options then
    TipoBusqueda := TipoBusqueda + [stMatchCase];

  with VentanaHija.reManuscrito do begin

    if frDown in FindDialog.Options then begin  //BUSQUEDA PARA ABAJO
      if SelLength <> 0 then   //si hay una palabra o frase seleccionada
        StartPos := SelStart + SelLength
      else
        StartPos := 0;   //en caso contrario, comenzar desde el principio

      ToEnd := Length(Text) - StartPos;  //ToEnd es la distancia entre StarPos y el final del texto

      FoundAt := FindText(FindDialog.FindText, StartPos, ToEnd, TipoBusqueda);
    end //end if
    else begin  //BUSQUEDA PARA ARRIBA
      if SelLength <> 0 then   //si hay una palabra o frase seleccionada
        StartPos := SelStart - 1
      else
        StartPos := SelStart;   //en caso contrario, comenzar desde el principio

      for z := 0 to StartPos do begin
        xText := Copy(Text,StartPos - z,Length(FindDialog.FindText));
        if FindDialog.FindText = xText then begin
          FoundAt := StartPos - z - 1; break; end; //end if
      end; //end for
    end; //end else

    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog.FindText);
    end //end if
    else begin
      MessageDlg('No se encontraron más equivalencias', mtInformation, [mbOK], 0);
      SelLength := 0;
    end; //end else
  end; //end with
end;


The thing is that this code obviously works only for the FindDialog as I refer to it's properties throughout the code. But how can I use the same code for both TFindDialog's OnFind event, and TReplaceDialog OnFind event?

I tried something like this but doesn't seem to work.

  if Sender is TFindDialog then
    Dialog := Sender as TFindDialog
  else if Sender is TReplaceDialog then
    Dialog := Sender as TReplaceDialog;

Thanks in advance.
0
Comment
Question by:pin_plunder
2 Comments
 
LVL 2

Accepted Solution

by:
mullet_attack earned 25 total points
ID: 2686194
as TReplaceDialog descends from TFindDialog, you can safely cast sender as a TFindDialog in both cases.

procedure TfrmPrincipal.FindDialogFind(Sender: TObject);
var
  VentanaHija: TfrmVentanaHija; FoundAt: LongInt;
  StartPos, ToEnd, z: integer; TipoBusqueda: TSearchTypes;
  xText: string;
begin
  with sender as TFindDialog do
begin
  VentanaHija := ActiveMDIChild as TfrmVentanaHija;
  FoundAt := -1;

 {Determinar el tipo de búsqueda}
  if frWholeWord in Options then
    TipoBusqueda := TipoBusqueda + [stWholeWord];
  if frMatchCase in Options then
    TipoBusqueda := TipoBusqueda + [stMatchCase];

  with VentanaHija.reManuscrito do begin

    if frDown in Options then begin  //BUSQUEDA PARA ABAJO
      if SelLength <> 0 then   //si hay una palabra o frase seleccionada
        StartPos := SelStart + SelLength
      else
        StartPos := 0;   //en caso contrario, comenzar desde el principio

      ToEnd := Length(Text) - StartPos;  //ToEnd es la distancia entre StarPos y el final del texto

      FoundAt := FindText(FindText, StartPos, ToEnd, TipoBusqueda);
    end //end if
    else begin  //BUSQUEDA PARA ARRIBA
      if SelLength <> 0 then   //si hay una palabra o frase seleccionada
        StartPos := SelStart - 1
      else
        StartPos := SelStart;   //en caso contrario, comenzar desde el principio

      for z := 0 to StartPos do begin
        xText := Copy(Text,StartPos - z,Length(FindText));
        if FindText = xText then begin
          FoundAt := StartPos - z - 1; break; end; //end if
      end; //end for
    end; //end else

    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindText);
    end //end if
    else begin
      MessageDlg('No se encontraron más equivalencias', mtInformation, [mbOK], 0);
      SelLength := 0;
    end; //end else
  end; //end with
end; // end do
end;
0
 

Author Comment

by:pin_plunder
ID: 2688529
I didnt' use the code you provided but you gave me a great idea, so thanks.

ah! the error with your code is that as I also use:
with VentanaHija.reManuscrito do begin

when FindText is called (refering to FindDialog.FindText -the text to finf-) it interprets it as the reManuscrito.FindText method.

though thanks a lot! and you got the points!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 169
Multiple image collision 13 80
SUM 2 INTEGER ARRAYS INTO 1 10 108
DBCtrlGrid, Delphi, Scroll 7 24
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question