Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-29-2016, 06:42 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 362
Default FindDialog and multiple valuelisteditors

i need for the user to click the find and for it to search thru all the VLEs stopping on the row and allow the users to edit it or click the find next.
_tidx and _vidx are global integers and on the finddialog show routine i initialize them back to zero.
How do i need to approach this issue for the find next and the Tabs/VLEs to walk thru them all and loop back around if necessary?
Code:
procedure TfrmMain.fDialogFind(Sender: TObject);
var
  _fText  : string;
  idx : integer;
begin
  if fDialog.FindText <> '' then
  begin
    _fText := stringreplace(UpperCase(trim(fDialog.FindText)), '_','-',[rfReplaceAll]);
    if _tidx > pgcntlCBL.PageCount - 1 then
      _tidx := 0;
    pgcntlCBL.ActivePageIndex := _tidx;
    case _tidx of
      0:
        begin
          for idx := _vidx to vleClient.RowCount - 1 do
          begin
            if Pos(_fText, vleClient.Keys[idx]) > 0 then
            begin
              vleClient.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      1:
        begin
          for idx := _vidx to vleCRM.RowCount - 1 do
          begin
            if Pos(_fText, vleCRM.Keys[idx]) > 0 then
            begin
              vleCRM.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      2:
        begin
          for idx := _vidx to vleFIN.RowCount - 1 do
          begin
            if Pos(_fText, vleFIN.Keys[idx]) > 0 then
            begin
              vleFIN.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      3:
        begin
          for idx := _vidx to vleCourt.RowCount - 1 do
          begin
            if Pos(_fText, vleCourt.Keys[idx]) > 0 then
            begin
              vleCourt.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      4:
        begin
          for idx := _vidx to vleMisc.RowCount - 1 do
          begin
            if Pos(_fText, vleMisc.Keys[idx]) > 0 then
            begin
              vleMisc.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      5:
        begin
          for idx := _vidx to vleCommon.RowCount - 1 do
          begin
            if Pos(_fText, vleCommon.Keys[idx]) > 0 then
            begin
              vleCommon.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      6:
        begin
          for idx := _vidx to vleOther.RowCount - 1 do
          begin
            if Pos(_fText, vleOther.Keys[idx]) > 0 then
            begin
              vleOther.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
      7:
        begin
          for idx := _vidx to vleSQL_Detail.RowCount - 1 do
          begin
            if Pos(_fText, vleSQL_Detail.Keys[idx]) > 0 then
            begin
              vleSQL_Detail.Row := idx;
              _vidx := idx;
              Break;
            end;
          end;
          inc(_tidx);
          _vidx := 0;
        end;
    else
      // page one again.
    end;
  end;
end;
  #2  
Old 07-08-2016, 01:08 AM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 200
Default

I've been working on a solution for a few days, hopefully this will solve your problem. The one option in the TFindDialog that I didn't incorporate was the Match Whole Word only, so you can either add that yourself, or change the FindDialog's Options to include frHideWholeWord.
(The code is too long to put in one post, so the rest will follow in the next post.

Code:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes, System.StrUtils,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.Grids,
  Vcl.ValEdit, Vcl.StdCtrls, Vcl.Buttons;

type
  TMatchedInstance = record
    MatchSuccess: Boolean;
    SearchText: String;
    MatchedPageControlIndex: Integer;
    MatchedVLERow: Integer;
    MatchedVLEMatchedOffset: Integer;
  end;

  TfrmMain = class(TForm)
    pgcntlCBL: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    TabSheet5: TTabSheet;
    TabSheet6: TTabSheet;
    TabSheet7: TTabSheet;
    TabSheet8: TTabSheet;
    vleClient: TValueListEditor;
    vleCRM: TValueListEditor;
    vleFIN: TValueListEditor;
    vleCourt: TValueListEditor;
    vleMisc: TValueListEditor;
    vleSQL_Detail: TValueListEditor;
    vleOther: TValueListEditor;
    vleCommon: TValueListEditor;
    FindButton: TBitBtn;
    FindDialog1: TFindDialog;
    procedure FindButtonClick(Sender: TObject);
    procedure FindDialog1Find(Sender: TObject);
  private
    FirstFind: Boolean;
    MatchedInstance: TMatchedInstance;
    function FindInValueListEditors(const FindText: String; MaxPageIndex: Integer; FindOptions: TFindOptions): TMatchedInstance; overload;
    function FindInValueListEditors(LastMatchedInstance: TMatchedInstance; MaxPageIndex: Integer; FindOptions: TFindOptions): TMatchedInstance; overload;
    function GetVLE(Index: Integer): TValueListEditor;
    function SameInstance(L, R: TMatchedInstance): Boolean;
    procedure CloneMatchedInstance(var L, R: TMatchedInstance);
    procedure FixSearchTerms(var Needle, Haystack: String; SearchOptions: TFindOptions);
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

{ TForm1 }

procedure TfrmMain.CloneMatchedInstance(var L, R: TMatchedInstance);
begin
   L.MatchSuccess := R.MatchSuccess;
   L.SearchText := R.SearchText;
   L.MatchedPageControlIndex := R.MatchedPageControlIndex;
   L.MatchedVLERow := R.MatchedVLERow;
   L.MatchedVLEMatchedOffset := R.MatchedVLEMatchedOffset;
end;

procedure TfrmMain.FindButtonClick(Sender: TObject);
begin
   FirstFind := True;
   FindDialog1.Execute;
end;

procedure TfrmMain.FindDialog1Find(Sender: TObject);
var
  VLE: TValueListEditor;
  SavedMatchedInstance: TMatchedInstance;
begin
   if FirstFind then
   begin
     FirstFind := False;
     if (FindDialog1.FindText <> '') then
     begin
       MatchedInstance := FindInValueListEditors(FindDialog1.FindText, pgcntlCBL.PageCount - 1, FindDialog1.Options);
       if MatchedInstance.MatchSuccess then
        begin
          pgcntlCBL.ActivePageIndex := MatchedInstance.MatchedPageControlIndex;
          VLE := GetVLE(MatchedInstance.MatchedPageControlIndex);
          VLE.SetFocus;
          VLE.Row := MatchedInstance.MatchedVLERow;
        end
        else
          ShowMessage('No Instance of '+FindDialog1.FindText+ ' found.');
     end;
   end
   else
   begin
     CloneMatchedInstance(SavedMatchedInstance, MatchedInstance);
     MatchedInstance := FindInValueListEditors(MatchedInstance, pgcntlCBL.PageCount - 1, FindDialog1.Options);
     if (MatchedInstance.MatchSuccess) and Not (SameInstance(SavedMatchedInstance, MatchedInstance)) then
     begin
       pgcntlCBL.ActivePageIndex := MatchedInstance.MatchedPageControlIndex;
       VLE := GetVLE(MatchedInstance.MatchedPageControlIndex);
       VLE.SetFocus;
       VLE.Row := MatchedInstance.MatchedVLERow;
     end;
   end;
end;
  #3  
Old 07-08-2016, 01:08 AM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 200
Default

Code:
function TfrmMain.FindInValueListEditors(const FindText: String; MaxPageIndex: Integer; FindOptions: TFindOptions): TMatchedInstance;
var
  VLE: TValueListEditor;
  Found: Boolean;
  CurPageControlIndex: Integer;
  I: Integer;
  FindPosition: Integer;
  NoInstanceFound: Boolean;
  Needle: String;
  Haystack: String;
begin
  Found := False;
  NoInstanceFound := False;
  if frDown in FindOptions then
    CurPageControlIndex := 0
  else
    CurPageControlIndex := MaxPageIndex;
  VLE := GetVLE(CurPageControlIndex);
  While (Not (Found)) do
  begin
    if NoInstanceFound then
       Break;
    if frDown in FindOptions then
    begin
      for I := 1 to VLE.RowCount - 1 do
      begin
        Needle := FindText;
        Haystack := VLE.Keys[I];
        FixSearchTerms(Needle, Haystack, FindDialog1.Options);
        FindPosition := Pos(Needle, Haystack);
        if FindPosition > 0 then
        begin
          Result.MatchSuccess := True;
          Result.SearchText := FindText;
          Result.MatchedPageControlIndex := CurPageControlIndex;
          Result.MatchedVLERow := I;
          Result.MatchedVLEMatchedOffset := FindPosition;
          Found := True;
          Break;
        end
        else
        begin
          if I = VLE.RowCount - 1 then
          begin
            Inc(CurPageControlIndex);
            if CurPageControlIndex < MaxPageIndex then
            begin
              VLE := GetVLE(CurPageControlIndex);
              Break;
            end
            else
            begin
              Result.MatchSuccess := False;
              Result.SearchText := FindText;
              NoInstanceFound := True;
              Break;
            end;
          end;
        end;
      end;
    end
    else
    begin
      VLE := GetVLE(CurPageControlIndex);
      for I := VLE.RowCount - 1 downto 1 do
      begin
        Needle := FindText;
        Haystack := VLE.Keys[I];
        FixSearchTerms(Needle, Haystack, FindDialog1.Options);
        FindPosition := pos(FindText, VLE.Keys[I]);
        if FindPosition > 0 then
        begin
          Result.MatchSuccess := True;
          Result.SearchText := FindText;
          Result.MatchedPageControlIndex := CurPageControlIndex;
          Result.MatchedVLERow := I;
          Result.MatchedVLEMatchedOffset := FindPosition;
          Found := True;
          Break;
        end
        else
        begin
          if I = 1 then
          begin
            Dec(CurPageControlIndex);
            if CurPageControlIndex > 0 then
            begin
              VLE := GetVLE(CurPageControlIndex);
              Break;
            end
            else
            begin
              Result.MatchSuccess := False;
              Result.SearchText := FindText;
              NoInstanceFound := True;
              Break;
            end;
          end;
        end;
      end;
    end;
  end;
end;

function TfrmMain.FindInValueListEditors(LastMatchedInstance: TMatchedInstance; MaxPageIndex: Integer; FindOptions: TFindOptions): TMatchedInstance;
var
  FirstRun: Boolean;
  Restarted: Boolean;
  PastFirst: Boolean;
  Needle: String;
  Haystack: String;
  VLE: TValueListEditor;
  CurRow: Integer;
  FindPosition: Integer;
  Found: Boolean;
  I: Integer;
  CurPageControlIndex: Integer;
begin
  FirstRun := True;
  Restarted := False;
  PastFirst := False;
  Found := False;
  CurPageControlIndex := LastMatchedInstance.MatchedPageControlIndex;
  While (Not Found) or (Restarted and PastFirst) do
  begin
    VLE := GetVLE(CurPageControlIndex);
    if frDown in FindOptions then
    begin
      if FirstRun then
      begin
        if LastMatchedInstance.MatchedVLERow = VLE.RowCount - 1 then
        begin
          Inc(CurPageControlIndex);
          if CurPageControlIndex > MaxPageIndex then
            CurPageControlIndex := 0;
          VLE := GetVLE(CurPageControlIndex);
          CurRow := 0;
        end
        else
          CurRow := LastMatchedInstance.MatchedVLERow + 1;
      end
      else
        CurRow := 0;
      for I := CurRow to VLE.RowCount - 1 do
      begin
        Needle := LastMatchedInstance.SearchText;
        Haystack := VLE.Keys[I];
        FixSearchTerms(Needle, Haystack, FindDialog1.Options);
        FindPosition := Pos(Needle, Haystack);
        FirstRun := False;
        if FindPosition > 0 then
        begin
          Result.MatchSuccess := True;
          Result.SearchText := LastMatchedInstance.SearchText;
          Result.MatchedPageControlIndex := CurPageControlIndex;
          Result.MatchedVLERow := I;
          Result.MatchedVLEMatchedOffset := FindPosition;
          Found := True;
          Break;
        end
        else
        begin
          if I = VLE.RowCount - 1 then
          begin
            Inc(CurPageControlIndex);
            if CurPageControlIndex > MaxPageIndex then
            begin
              CurPageControlIndex := 0;
              Restarted := True;
            end
            else if (CurPageControlIndex >= LastMatchedInstance.MatchedPageControlIndex) and (Restarted) then
              PastFirst := True;
          end;
          if (Restarted) and (PastFirst) then
          begin
            Result.MatchSuccess := False;
            Result.SearchText := LastMatchedInstance.SearchText;
            Break;
          end;
        end;
      end;
    end
    else
    begin
      if FirstRun then
      begin
        if LastMatchedInstance.MatchedVLERow = 0 then
        begin
          Dec(CurPageControlIndex);
          if CurPageControlIndex < 0  then
            CurPageControlIndex := MaxPageIndex;
          VLE := GetVLE(CurPageControlIndex);
          CurRow := VLE.RowCount - 1;
        end
        else
          CurRow := LastMatchedInstance.MatchedVLERow - 1;
      end
      else
        CurRow := VLE.RowCount - 1;
      for I := CurRow downto 1 do
      begin
        Needle := LastMatchedInstance.SearchText;
        Haystack := VLE.Keys[I];
        FixSearchTerms(Needle, Haystack, FindDialog1.Options);
        FindPosition := Pos(Needle, Haystack);
        FirstRun := False;
        if FindPosition > 0 then
        begin
          Result.MatchSuccess := True;
          Result.SearchText := LastMatchedInstance.SearchText;
          Result.MatchedPageControlIndex := CurPageControlIndex;
          Result.MatchedVLERow := I;
          Result.MatchedVLEMatchedOffset := FindPosition;
          Found := True;
          Break;
        end
        else
        begin
          if I = 1 then
          begin
            Dec(CurPageControlIndex);
            if CurPageControlIndex < 0 then
            begin
              CurPageControlIndex := MaxPageIndex;
              Restarted := True;
            end
            else if (CurPageControlIndex <= LastMatchedInstance.MatchedPageControlIndex) and (Restarted) then
              PastFirst := True;
          end;
          if (Restarted) and (PastFirst) then
          begin
            Result.MatchSuccess := False;
            Result.SearchText := LastMatchedInstance.SearchText;
            Break;
          end;
        end;
      end;
    end;
  end;
end;

procedure TfrmMain.FixSearchTerms(var Needle, Haystack: String; SearchOptions: TFindOptions);
begin
  if frMatchCase in SearchOptions then
  begin
    Needle := Needle;
    Haystack := HayStack;
  end
  else
  begin
    Needle := UpperCase(Needle);
    Haystack := UpperCase(HayStack);
  end;
end;

function TfrmMain.GetVLE(Index: Integer): TValueListEditor;
begin
  case Index of
    0: Result := vleClient;
    1: Result := vleCRM;
    2: Result := vleFIN;
    3: Result := vleCourt;
    4: Result := vleMisc;
    5: Result := vleCommon;
    6: Result := vleOther;
    else Result := vleSQL_Detail;
  end;
end;

function TfrmMain.SameInstance(L, R: TMatchedInstance): Boolean;
begin
  Result := (L.SearchText = R.SearchText) and (L.MatchedPageControlIndex = R.MatchedPageControlIndex) and
    (L.MatchedVLERow = R.MatchedVLERow) and (L.MatchedVLEMatchedOffset = R.MatchedVLEMatchedOffset);
end;

end.
  #4  
Old 07-13-2016, 09:32 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 362
Default Thank you so much...

I really appreciate all the work that went into that and a big help.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:54 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.