?
Solved

Easy FindDialog question

Posted on 2000-04-04
2
Medium Priority
?
385 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 75 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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