How to implement "previous file" and "next file" browsing features to view files of a given type ?

Here's what I have in mind :

+--------------------------------------------------------------+
| X Visual file browsing application       |_|#|X|      |
+--------------------------------------------------------------+
|     +---------------+        +--------------+                        |
|     : <  Previous :        :    Next    >   :                        |
|     +---------------+        +--------------+                        |
| +-----------------------------------------------------------+ |
| :                                                                                 : |
| :                                                                                 : |
| :                                                                                 : |
| :                                                                                 : |
| :           Proper visual component                        : |
| :           which shows the contents                       : |
| :             of the current file                                      : |
| :                                                                                 : |
| :                                                                                 : |
| :                                                                                 : |
| :                                                                                 : |
| +-----------------------------------------------------------+ |
+--------------------------------------------------------------+

At startup, the user opens a file of the required
type from the folder of his choice and the contents
of this file appears in the visual component.

Then, when the user clicks on the previous/next
button, the contents of the (alphabetically speaking)
previous/next file of that type contained
in the same folder appears in the visual component.

Comments :

If it doesn't take too long,
I would like the previous/next file search
to be redone every time a button is clicked
to prevent possible problems in case
a file is deleted/added in the meantime...

(Problems which could arise if a list of the files
contained in the folder is stored in a variable
once and for all)

Thank you,

Basile ;) Absolute beginner in Delphi 6 ;)
BazillaAsked:
Who is Participating?
 
lmikleConnect With a Mentor Commented:
At first then you open a file you mast save a full file name in some temporary variable. Then user click on Next or Previouse button you mast read a current file list and determine file name of next or previouse file.

TForm1 = class(TForm)
  dlgOpen : TOpenDialog;
...
private
  CurrFN : String;
  function GetFilesList : TStringList;
...
end;

procedure TForm1.btOpen1Click(Sender : TObject);
var
  L : TStringList;
  Idx : Integer;
begin
  // Here you open file
  if dlgOpen.Execute Then
  begin
   ...
   // and now save file name
   CurrFN := dlgOpen.FileName;
   // and refresh Next and Prev. buttons state
   L := GetFilesList;
   if L.Find(ExtractFileName(CurrFN),Idx) then
     Begin
       // File in list was found
       btNext.Enabled := Idx < L.Count-1;
       btPreviouse.Enabled := Idx > 0;
     End
   else
     Begin
       // Dile in list not found - disable both  buttons
       btNext.Enabled := False;
       btPreviouse.Enabled := False;
     End;
  end;
  FreeAndNil(L);
...
end;

// User Click on Next or Prev. button.
// Use a common event handler for both buttons
procecdure TForm1.btNexPrevtClick(Sender : TObject);
var
  L : TStringList;
  Idx : Integer;
begin
  L := GetFilesList;
  if L.Find(ExtractFileName(CurrFN),Idx) then
    begin
      // We use a TButton for Next and Prev. buttons
      If (Sender As TButton).Name = 'btNext' then
        begin
          // User click on Next button
          Inc(Idx); // get next file name
        end
      else
        begin
          // User click on Prev. button
          Dec(Idx); // get previouse file name
        end

      // get new file name
      CurrFN := ExtractfilePath + L[Idx];
      // Here you must open this file
      .....

      // refresh Next and Prev. buttons status
      btNext.Enabled := Idx < L.Count-1;
      btPreviouse.Enabled := Idx > 0;
    end
  else
    begin
      // file not found
      ShowMessage('Current file not found in a list. Can''t determine nexr or previouse file.');
      btNext.Enabled := False;
      btPrevoiuse.Enabled := False;
    end;
  FreeAndNil(L);
end;

// get list function
function TForm1.GetfilesList : TStringList;
var
  Path : String;
  iFound : Integer;
  SR : TSearchRec;
begin
  Result := TStringList.Create;
  Path := ExtractFilePath(CurrFN);
  iFound := FindFirst(Path+'*.'+ExtractFileExt(CurrFile), faAnyfile, SR);
  While iFound = 0 Do
    Begin
      if (SR.Attr And faDirectory) = 0 Then
        begin
          Result.Add(SR.Name);
        end;
      iFound := FindNext(SR);
    End;
  FindClose(SR);
  Result.Sort;
end;
0
 
BazillaAuthor Commented:
Thank you LMIKLE !!!

Exactly what I was looking for and it works great !

Regards,

Basile ;) Absolute beginner in Delphi 6 ;)

----------------------------------------------------------------------------

Here's a slightly extended version of LMIKLE's solution for future readers :

----------------------------------------------------------------------------

At first when you open a file you must save the full file name in some temporary variable.
When the user clicks on the Next or Previous button you must read a current file list
and determine the filename of the next or previous file.

type
 TForm1 = class(TForm)
  btOpen: TButton;
  btPrevious: TButton;
  btNext: TButton;
  dlgOpen: TOpenDialog;
  procedure btOpenClick(Sender: TObject);
  procedure btNextPrevClick(Sender: TObject);
...
private
 CurrFN : String;
 function GetFilesList : TStringList;
...
end;

// Opening of the first file
procedure TForm1.btOpenClick(Sender : TObject);
var
 L : TStringList;
 Idx : Integer;
begin
 // Here you open the file
 if dlgOpen.Execute Then
 begin
  ...
  // save its filename
  CurrFN := dlgOpen.FileName;
  // and get a list of the files of that type in the current folder
  L := GetFilesList;
  // so we can refresh the state of the Next and Previous buttons
  if L.Find(ExtractFileName(CurrFN),Idx) then
    Begin
      // The current file was found at position Idx
      // in the list of files of that type
      // and the buttons are enabled accordingly
      btNext.Enabled := Idx < L.Count-1;
      btPrevious.Enabled := Idx > 0;
    End
  else
    Begin
      // It's the only file of that type
      // both buttons are disabled
      btNext.Enabled := False;
      btPrevious.Enabled := False;
    End;
 end;
 FreeAndNil(L);
...
end;

// When the user clicks on the Next or Previous button
// we use a common event handler for both buttons
procedure TForm1.btNextPrevClick(Sender : TObject);
var
 L : TStringList;
 Idx : Integer;
begin
 L := GetFilesList;
 if L.Find(ExtractFileName(CurrFN),Idx) then
   begin
     If (Sender As TButton).Name = 'btNext' then
       begin
         // User clicks on the Next button
         Inc(Idx); // get next file name
       end
     else
       begin
         // User clicks on the Previous button
         Dec(Idx); // get previous file name
       end
     // get new file name
     CurrFN := ExtractfilePath(CurrFN) + L[Idx];
     // Here you must open this file
     ...
     // refresh Next and Prev. buttons status
     btNext.Enabled := Idx < L.Count-1;
     btPrevious.Enabled := Idx > 0;
 end
 else
   begin
     // Current file not found
     ShowMessage('No current file found. Can''t determine next or previous file.');
     btNext.Enabled := False;
     btPrevious.Enabled := False;
   end;
 FreeAndNil(L);
end;

// To get a list of the files of that type
// in the current folder
function TForm1.GetFilesList : TStringList;
var
 Path : String;
 iFound : Integer;
 SR : TSearchRec;
begin
 Result := TStringList.Create;
 Path := ExtractFilePath(CurrFN);
 iFound := FindFirst(Path+'*'+ExtractFileExt(CurrFile), faAnyfile, SR);
 While iFound = 0 Do
   Begin
     if (SR.Attr And faDirectory) = 0 Then
       begin
         Result.Add(SR.Name);
       end;
     iFound := FindNext(SR);
   End;
 FindClose(SR);
 // The filenames are sorted alphabetically
 Result.Sort;
end;
0
All Courses

From novice to tech pro — start learning today.