[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Easy FindDialog question

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

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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