Solved

Easy FindDialog question

Posted on 2000-04-04
2
377 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 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