?
Solved

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

Posted on 2003-03-25
2
Medium Priority
?
160 Views
Last Modified: 2010-04-03
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 ;)
0
Comment
Question by:Bazilla
[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 3

Accepted Solution

by:
lmikle earned 300 total points
ID: 8205496
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
 

Author Comment

by:Bazilla
ID: 8216780
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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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