Solved

Easy FindDialog question

Posted on 2000-04-04
2
351 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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now