How to put scrollbox on a the Tabs of a PageControl.TabSheet?

IT79637
IT79637 used Ask the Experts™
on
Hi,
I'm on a Win XP Pro SP3 using Delphi7 Ent.

I do this as a hobby so please bear with me!
My Form is designed  as follows:

Form1
   Panel1
   ...
   Panel2
      PageControl1
         Image1.. multiple times.

In the program, I dynamically create TabSheets and I palce an TImage control on each of them successfully.

I need to add a Scroll box to each TabSheet and then add multiple TImage controls to each ScrollBox.

I assume the layering is as follows from above:

Panel2
   TPageControl
      TTabSheet - create at runtime
        TScrollBar -  NEW CONTROL  layer so I can have Scroll Bars on each Tab.
            TImage - created multiple times at runtime.

How do I put Scroll Bars on TabSheets and then put TImage on the TScrollBar?

Code is below with comments...
         

var
  fForm: TForm1;
  PageControl1 : PageControl;
  ScrollBox: TScrollBox;
  Tab: TTabSheet;
  Image: TImage;
  arPathFileName : array of string;
  arPath : array of String;
  arSetName : array of String;
implementation
.........
procedure TForm.btnDisplayPicsClick(Sender: TObject);
var
 myFile : TextFile;
  i: Integer;
  strWkFile: string;
  strSetName, strPathFileName,text : string;
  iNoSets, iPageIdx, iPrevPageNo : integer;
  iNoRows, iNoCols : integer;
  iActTop, iActLeft, iNoCtls,iNoImages : integer;
BEGIN
  // Make TabSheets and add caption to each
  for i := 0 to 4 do begin
    Tab := TTabSheet.Create(Self);
    Tab.PageControl := PageControl1;
    Tab.Width := 120;
     case i of
       0 : Tab.Caption := 'Vacation 2001';
       1 : Tab.Caption := 'Vacation 2003'  ;
       2 : Tab.Caption := 'Vacation 2005';
       3 : Tab.Caption := 'Vacation 2007';
       4 : Tab.Caption := 'Vacation 2009';
     end;
  end;

// HERE IS WHERE I WANT TO ADD TScrollBox to each TabSheet
// DON'T KNOW HOW TO DO THIS!!
//I GET ALL MESSED UP HERE!!!
  for i := 0 to 4 do begin
    PageControl1.ActivePageIndex := i;  // Select 1st TabSheet.
    with TScrollBox.Create(Self) do begin  
      //Parent := PageControl1.ActivePage;
      Parent := TScrollBox;
      Align := alClient;
    end;
  end;

  //Read Path & FileName into an aray for further processing
  i:= 0;
  strWkFile := Trim(edPathFile.Text);
  AssignFile(myFile, strWkFile);
  Reset(myFile);
  while not Eof(myFile) do begin
     ReadLn(myFile, text);
     arPathFileName[i] := Trim(text);
     Inc(i);
  end;
  CloseFile(myFile);
  iNoImages := i;


  //Extract Path & SetName
  for i := 0 to iNoImages-1  do begin
   1. strFileName := Get file name.
   2. strPath     := Get Path
   3. strSetName  := Get the last directory in the path structure - Vaction2001,            Vacation2003, etc...
  end; 

  //Initialize variables
  ....
  ....
  ....

  // Loop through all images in array
  for i := 0 to iNoImages-1  do begin    
    iPageIdx := WhatTab(LeftStr(arSetName[i],1)); //Find what TabSheet to put image on.
    
    //Place TImage on TabSheet or PageControl???  I not sure which one?
    with TImage.Create(Self) do begin              //owner is the form?
      PageControl1.ActivePageIndex := iPageIdx;    //zero based 
      Parent := PageControl1.ActivePage;
      Name   := 'Image' + IntToStr(i);
      Width  := 600;
      Height := 200;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column
     
      //Test if max number of columns and move 1 row down when at max
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin //1007
        iActTop  := iActTop + 185; //Start a new row (down)
        iActLeft := 6;            //Reset column to column 1
      end;

    end; //end with TImage
  end;  // end for loop

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ephraim WangoyaSoftware Engineer

Commented:
Use this
procedure TForm.btnDisplayPicsClick(Sender: TObject);
var
  myFile : TextFile;
  i: Integer;
  strWkFile: string;
  strSetName, strPathFileName,text : string;
  iNoSets, iPageIdx, iPrevPageNo : integer;
  iNoRows, iNoCols : integer;
  iActTop, iActLeft, iNoCtls,iNoImages : integer;
  SC: TScrollBox;
BEGIN
  // Make TabSheets and add caption to each
  for i := 0 to 4 do begin
    Tab := TTabSheet.Create(Self);
    Tab.PageControl := PageControl1;
    Tab.Width := 120;
    SC := TScrollBox.Create(Self);
    SC.Parent := Tab;
    SC.Align := alClient;
     case i of
       0 : Tab.Caption := 'Vacation 2001';
       1 : Tab.Caption := 'Vacation 2003'  ;
       2 : Tab.Caption := 'Vacation 2005';
       3 : Tab.Caption := 'Vacation 2007';
       4 : Tab.Caption := 'Vacation 2009';
     end;
  end;
  ............

Open in new window

Commented:
  for i := 0 to 4 do begin
    PageControl1.ActivePageIndex := i;  // Select 1st TabSheet.
    with TScrollBox.Create(Self) do begin  
      Parent := PageControl1.ActivePage;
      Name := 'ScrlBox' + IntToStr(i)
      Align := alClient;
      Visible := True;
    end;
  end;

Open in new window


  // Loop through all images in array
  for i := 0 to iNoImages-1  do begin    
    iPageIdx := WhatTab(LeftStr(arSetName[i],1)); //Find what TabSheet to put image on.
    
    //Place TImage on TabSheet or PageControl???  I not sure which one?
    with TImage.Create(Self) do begin             
      PageControl1.ActivePageIndex := iPageIdx;    
      Parent := PageControl1.ActivePage; // This line is wrong, parent must be ScrollBox !!!!!!!!!!!!!
      Name   := 'Image' + IntToStr(i);
      Width  := 600;
      Height := 200;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column
     
      //Test if max number of columns and move 1 row down when at max
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin //1007
        iActTop  := iActTop + 185; //Start a new row (down)
        iActLeft := 6;            //Reset column to column 1
      end;
      Visible := True;
    end; //end with TImage
  end;  // end for loop

Open in new window

Commented:
As far as I understood you try to do something like pictures preview. Then look as it can be done by other method
TListView-image-file-preview.zip

Commented:
Also you can check line Parent := PageControl1.ActivePage;

function GetScrollBoxAtTabSheet(ts: TTabSheet): TScrollBox;
var
  I: Integer;
begin
  for I := 0 to ts.ComponentCount - 1 do
   if ts.Components[I].ClassNameIs('TScrollBox') then
     Result := TScrollBox(ts.Components[I]);
end;

....

    with TImage.Create(Self) do begin             
      PageControl1.ActivePageIndex := iPageIdx;    
      Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage); // This line
      Name   := 'Image' + IntToStr(i);
      Width  := 600;
      Height := 200;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column
     
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin
        iActTop  := iActTop + 185; 
        iActLeft := 6;         
      end;

      Visible := True;

    end;

Open in new window

Author

Commented:
I tried to implement Author:VahaC  Date:08/06/11 02:19 PM code from above.

The image below shows the output of running the program using this code.  I added code to list all the controls.  See image.



It seems the main problem is getting the TImage controls on the correct ScrollBox.

Thanks.
[b]1.The following code was added to produce the image below:[/b] 
procedure TfGallery.Button1Click(Sender: TObject);
var
 i : integer;
begin
  LIstBox1.Clear;
  for I := 0 to ComponentCount -1 do
    ListBox1.Items.Add(Components[i].Name);
end;

------------------------------------------------------------------------------------------------------------------------------
[b]2. Fuction to set ScollBox Parent to TabSheet or ActivePage ????  Not sure what?
This call always fails because tsComponentCount always = 0. Reaults "for I=0 to -1..."[/b]
function GetScrollBoxAtTabSheet(ts: TTabSheet): TScrollBox;
var
  I: Integer;
begin
  for I := 0 to ts.ComponentCount - 1 do begin
   if ts.Components[I].ClassNameIs('TScrollBox') then
     Result := TScrollBox(ts.Components[I]);
  end;
end;

------------------------------------------------------------------------------------------------------------------------------
[b]3. Main Processing loop
Program fails on this statement "   Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage); because ComponentCount=0 in fuction. See above.[/b]

procedure TfGallery.btnDisplayGallerlyClick(Sender: TObject);
var
  i,j: Integer;
  strWkFile, strSuccess: string;
  strSetName, strPathFileName,text : string;
  iNoSets, iPageIdx, iPrevPageNo : integer;
  iNoRows, iNoCols : integer;
  iActTop, iActLeft, iNoCtls,iNoImages : integer;
begin
//*************************************************************************
//***                    MAIN LOOP TO TO SETUP FORM                    ***
//***  1. Create TabSheet
//***  2. Add Caption to TabSheet
//***  3. With TScrollbox
//***     a. Create ScrollBox0 to ScrollBox4
//***     b. Set Color, Align & Visible properties
//*************************************************************************
  for i := 0 to 4 do begin
  //Create Tab Sheets& place ScrollBox on each Tab. Assign Captions
   Tab := TTabSheet.Create(Self);
   Tab.PageControl := PageControl1;
   PageControl1.ActivePageIndex := i;
    case i of
       0 : Tab.Caption := 'Vacation-1001';
       1 : Tab.Caption := 'Vacation-1002';
       2 : Tab.Caption := 'Vacation-1003';
       3 : Tab.Caption := 'Vacation-1004';
       4 : Tab.Caption := 'Vacation-1005';
    end;

    with TScrollBox.Create(Self) do begin
      Parent := PageControl1.ActivePage;
      Name := 'ScrlBox' + IntToStr(i);
      Color := clMedGray;
      Align := alClient;
      Visible := True;
    end;
  end;

//*************************************************************************
//***  Path to file that contains the path and image file name
//***     c:\temp\Vacation-2001\image001.jpg to image100.jpg
//***     c:\temp\Vacation-2002\image001.jpg to image125.jpg
//*************************************************************************
  strPathFileName := Trim(edPathFile.Text);

//*************************************************************************
//***  1. Open & loop through input file to EOF.
//***  2. Count the number of images iNoImages".
//***     c:\temp\Vacation-2001\image001.jpg to image100.jpg
//***     c:\temp\Vacation-2002\image001.jpg to image125.jpg
//***  3. Build Arrays from data.
//***       arPathFileName = Full Path & File Name.
//***       arSetName =  Last node of directory e.g. Vacation-2001 which is
//***                     used for Tabs Caption.
//***  4. In the example here, 225 images for first two Tabs
//*************************************************************************
  iNoImages := LoadImagesNamesToArrayFromFile(strPathFileName);

//Initialize variables
  iPrevPageNo := 0; // Setup up control break processing for TabSets.
  iNoRows := 3;     // Not used, number of rows determined by no images in image set
  iNoCols := 8;
  iActTop := 6;     //  Initial Image1.Top position  on each TabSheet
  iActLeft:= 6;     //  Initial Image1.Left position on each TabSheet

//*************************************************************************
//***           MAIN LOOP TO DISPLAY IMAGES STARTS HERE                 ***
//*************************************************************************
  for i := 0 to iNoImages-1  do begin    // Loop through all images in array containing path & file name.

    //Check if time to move to the next TabSheet
    iPageIdx := WhatTab(LeftStr(arSetName[i], 1)); //Find tab image goes on
    if iPrevPageNo <> iPageIdx then begin          //Check for tab page break & reset
      iActTop := 6;   // Reset Top position
      iActLeft:= 6;   // Reset Left postion
      iPrevPageNo := iPageIdx;  // Part of "control break" processing
    end;
   
    //Create images on TabSheets & ScrollBox
    with TImage.Create(Self) do begin
      PageControl1.ActivePageIndex := iPageIdx;//this value is 0,1,2,3,4 as loop through images
      Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage); // This line
      Name   := 'Image' + IntToStr(i);
      Width  := 600;
      Height := 200;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column

      //When column position excedes 1007, then start a new row directly below
      // 8columns with width 125 + 6 (left offset) +1 
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin //(8*125) +6+1 = 1007
        iActTop  := iActTop + 185;   //Start a new row (down)
        iActLeft := 6;              //Reset column position to column 1
      end;
     // sleep(250);
    end; //end with TImage...
  end;  // end for loop
end;

Open in new window

DelphiControlsLayering4.jpg

Commented:
I do not know from where you take datains, thats why i wrote a simple example that scan a folder with the program and creates tabs (subfolders) and Timages (pictures).
Everything work good.

Unpack and look hoq it work.
TImage-Pict-Thumb.zip

Author

Commented:
:VahaC  

When I compile, I get the error: "File not found PngImage.dcu".  Will you please re-post the zip file with the missing file?

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

Is there a tutorials somewhere that very clearly explains how to set Parent relationships among controls in the layering of controls on a form, such as I have done.
TForm
 TPanel
   TPageControl
     TScrollBox
        TImage1...Timage100
     TScrollBox
        TImage101...Timage200

I'm not a professional programmer.  I was an Analyst when I was working.  I spend my days writing functional specifications, occasionally  leading small projects (3-5 programmers or Sr. programmers), or a Lead Analyst supervising a team of programmers & analysts doing daily grind of Business Application Production Support.  Soooo, I not "technical" by any "stretch of the imagination".  I never wrote code on a consistent daily basis.  I'm just a rookie programmer at home.

So, would you please consider rewriting your program using the control structure layering  as defined in the Code Section below?  I know that  it is a lot to ask of you, but it would really help me out!!!

For now, I'm just stuck on how to make TScrollBox the Parent of TImage(s).  In my previous comment, I 'm having problems on lines 108 through 111.  But specifically line 110: Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage);.

I'll even attach a nice image to use (H=170, W=113)!

Thanks for your help and patience!


Panel1 - two buttons: (1) to execute the code and (2) an Exit button 
Panel2
  PageControl1
    TabSheet1      <<< starting here all controls are created at run time >>>
        ScrollBox1
           Image1
    TabSheet2
       ScrollBox2
          Image2
    TabSheet3
        ScrollBox3
           Image3    

TTabSheet, TScrollBoxes and TImage are created at run time.

Open in new window

beach-ocean-tree.bmp

Commented:
If you want GetScrollBoxAtTabSheet work properly
you must write
  for i := 0 to 4 do begin
    PageControl1.ActivePageIndex := i;  
    with TScrollBox.Create(Self) do begin  
      Parent := PageControl1.ActivePage; //by a parent must be set TabSheet
      Name := 'ScrlBox' + IntToStr(i)
      Align := alClient;
      Visible := True;
    end;
  end;

Open in new window


    with TImage.Create(Self) do begin             
      PageControl1.ActivePageIndex := iPageIdx;    
      Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage);//by a parent must be set SrollBox
      Name   := 'Image' + IntToStr(iPageIdx) + '_' + IntToStr(i);
      Width  := 600;
      Height := 200;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column
     
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin
        iActTop  := iActTop + 185; 
        iActLeft := 6;         
      end;
    end;

Open in new window


Your structure
Panel1 - two buttons: (1) to execute the code and (2) an Exit button 
Panel2
  PageControl1
    TabSheet1   parent := PageControl1   <<< starting here all controls are created at run time >>>
        ScrollBox1 parent := TabSheet1
           Image1  parent := ScrollBox1
    TabSheet2  parent := PageControl1  
       ScrollBox2  parent := TabSheet2
          Image2  parent := ScrollBox2
    TabSheet3  parent := PageControl1
        ScrollBox3  parent := TabSheet3
           Image3   parent := ScrollBox3  

Open in new window


When I compile, I get the error: "File not found PngImage.dcu".  Will you please re-post the zip file with the missing file?
In such case delete PNGImage from uses and check line
    if FindFirst(ExtractFilePath(Application.ExeName) + PageControl1.Pages[I].Caption + '\*', faAnyFile, FS) = 0 then

Open in new window

to
    if FindFirst(ExtractFilePath(Application.ExeName) + PageControl1.Pages[I].Caption + '\*.bmp', faAnyFile, FS) = 0 then

Open in new window

in such case program load to timages only files with extantion "bmp"
Also you can copy folders with your pictures to app folder.

PS
Sorry for my bad english

Author

Commented:
When calling GetScrollBoxAtTabSheet(PageControl1.ActivePage), I'm getting error: Undeclared Identifier (Self) in the function.


The code is below.
WHERE FUNCTIONS ARE DEFINED IN THE PROGRAM
------------------------------------------
private
    { Private declarations }
  public
    { Public declarations }
  end;
    function GetScrollBoxAtTabSheet(ts: TTabSheet): TScrollBox;
    function WhatTab (strName: string) : integer;
    function LoadImagesNamesToArrayFromFile(Path:string):integer;
    function ParsePath_FileName_SetName(iNoImages:integer): string;

FUNCTION: GetScrollBoxAtTabSheet
---------------------------------
function GetScrollBoxAtTabSheet(ts: TTabSheet): TScrollBox;
var
  I: Integer;
begin
  for i := 0 to 4 do begin
    PageControl1.ActivePageIndex := i;  
    with TScrollBox.Create(Self) do begin  
      Parent := PageControl1.ActivePage; //by a parent must be set TabSheet
      Name := 'ScrlBox' + IntToStr(i);
      Align := alClient;
      Visible := True;
    end;
  end;
end;

WHERE FUNCTION IS CALLED IN PROGRAM
-----------------------------------
for i := 0 to iNoImages-1  do begin    // Loop through all images in file

    //Check if time to move to the next TabSheet
    iPageIdx := WhatTab(LeftStr(arSetName[i], 1)); //Find tab image goes on
    if iPrevPageNo <> iPageIdx then begin          //Check for tab page break & reset
      iActTop := 6;   // Reset Top position
      iActLeft:= 6;   // Reset Left postion
      iPrevPageNo := iPageIdx;  // Part of "control break" processing
    end;
   
    //Create images on TabSheets & ScrollBox
    with TImage.Create(Self) do begin             
      PageControl1.ActivePageIndex := iPageIdx;    
      Parent := GetScrollBoxAtTabSheet(PageControl1.ActivePage);  <<<--- HERE  //by a parent must be set SrollBox
      Name   := 'Image' + IntToStr(i);
      Width  := 113;
      Height := 173;
      Top    := iActTop;
      Left   := iActLeft;
      strPathFileName := arPathFileName[i];
      Picture.LoadFromFile(strPathFileName);
      Refresh;
      iActLeft := iActLeft + 125; //Move right 1 column

      //When column position exceedes 1007, then start a new row directly below
      // 8columns with width 125 + 6 (left offset) +1 
      if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin //(8*125) +6+1 = 1007
        iActTop  := iActTop + 185;   //Start a new row (down)
        iActLeft := 6;              //Reset column position to column 1
      end;
    end; //end with TImage...

Open in new window

Commented:
To check this error yo must write
    with TScrollBox.Create(YOUR_FORM) do begin  

function GetScrollBoxAtTabSheet(ts: TTabSheet): TScrollBox;
var
  I: Integer;
begin
  for i := 0 to 4 do begin
    PageControl1.ActivePageIndex := i;  
    with TScrollBox.Create(Self) do begin  
      Parent := PageControl1.ActivePage; //by a parent must be set TabSheet
      Name := 'ScrlBox' + IntToStr(i);
      Align := alClient;
      Visible := True;
    end;
  end;
end;

Open in new window

If you will use your variant of GetScrollBoxAtTabSheet, app will create new ScrollBox for every TImage

Upload your project and i try to check your errors.

Author

Commented:
Here is a zip file of the program.

I've created a file which contains the path and file name of the image to be put on the ScrollBox.  This file as well as the directories where the thumb-nails images are in the zip file.

Read "READMEFIRST.txt" because you have to drop some directories and a file in C:\tmp\.  You can change it to C:\temp\, but you will have to update the path in the edit box at the top of the form and the path in Vacation.txt.  It would be much easier for you to use  C:\tmp\.  This code really sucks because I've been trying out a lot of things.  There is a lot of extra variable definition in it, etc....

Right now the program abends in function: GetScrollBoxAtTabSheet on this line of code: PageControl1.ActivePageIndex := i.

Please let me know what you find!

Thanks.
Gallery-AddTabs-Images03.zip

Commented:
you want to display all pictures that are in "c: \ tmp" or only those that are in the file "Vacation.txt"?

Author

Commented:
I only want to display the "th_cover.bmp" image.   I use the file input to capture Paths and the name of the thumb picture.  In this case there will be 1 on each tab.  Once the program is running, I plan on viewing the pics using windows pic and fax viewer....

Commented:
I rewrite your program
this is unit
unit uGallery;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, StrUtils, Buttons;

type
  TfGallery = class(TForm)
    RzPanel1: TPanel;
    btnExit: TButton;
    btnDisplayGallerly: TButton;
    Label1: TLabel;
    RzLabel1: TLabel;
    Label2: TLabel;
    edPathFile: TEdit;
    RzPanel2: TPanel;
    PageControl1: TPageControl;
    ScrollBox1AAA: TScrollBox;
    ListBox1: TListBox;
    procedure btnExitClick(Sender: TObject);
    procedure btnDisplayGallerlyClick(Sender: TObject);
  private
    { Private declarations }
  public
     { Public declarations }
  end;
var
  fGallery: TfGallery;
implementation

{$R *.dfm}

procedure TfGallery.btnDisplayGallerlyClick(Sender: TObject);
var
  I: Integer;
  iPrevPageNo : integer;
  iNoRows, iNoCols : integer;
  iActTop, iActLeft : integer;

  TabSht: TTabSheet;
  ScrlB: TScrollBox;
  files_lst: TStringList;
  old_folder, cur_folder: string;
begin
  files_lst := TStringList.Create;
  try
    files_lst.LoadFromFile(Trim(edPathFile.Text));
    old_folder := '';
    for I := 0 to files_lst.Count - 1 do
    begin
      cur_folder := LowerCase(ExtractFileName(ExtractFileDir(files_lst[I])));
      if cur_folder <> old_folder then
      begin
        old_folder := cur_folder;
        TabSht := TTabSheet.Create(Self);
        TabSht.PageControl := PageControl1;
        TabSht.Caption := cur_folder;
        ScrlB := TScrollBox.Create(Self);
        with ScrlB do
        begin
          Parent := TabSht;
          Name := 'ScrlBox' + IntToStr(TabSht.PageIndex);
          Color := clMedGray;
          Align := alClient;
          Visible := True;
        end;
        iPrevPageNo := 0; // Setup up control break processing for TabSets.
        iNoRows := 3;     // Not used, number of rows determined by no images in image set
        iNoCols := 8;
        iActTop := 6;     //  Initial Image1.Top position  on each TabSheet
        iActLeft:= 6;     //  Initial Image1.Left position on each TabSheet

        if FileExists(files_lst[I]) then
          with TImage.Create(Self) do
          begin
            Parent := ScrlB;
            Name   := 'Image' + IntToStr(I);
            Width  := 113;
            Height := 173;
            Top    := iActTop;
            Left   := iActLeft;
            Picture.LoadFromFile(files_lst[I]);
            Refresh;
            iActLeft := iActLeft + 125; //Move right 1 column

            //When column position exceedes 1007, then start a new row directly below
            // 8 columns with width 125 + 6 (left offset) +1
            if  (iActLeft > (((iNoCols-1)*125) + 6 + 1)) then begin //(8*125) +6+1 = 1007
              iActTop  := iActTop + 185;   //Start a new row (down)
              iActLeft := 6;              //Reset column position to column 1
            end;
          end; //end with TImage...
      end;  // end for loop
    end;
  finally
    files_lst.Free;
  end;
end;

procedure TfGallery.btnExitClick(Sender: TObject);
begin
  close;
end;

end.

Open in new window


this is project gallery.ZIP

Commented:
Please do some correction at my code

delete line #75
if FileExists(files_lst[I]) then

Open in new window


and correct this
    for I := 0 to files_lst.Count - 1 do
    begin
      cur_folder := LowerCase(ExtractFileName(ExtractFileDir(files_lst[I])));
      if cur_folder <> old_folder then

Open in new window

to
    for I := 0 to files_lst.Count - 1 do
    begin
      if not FileExists(files_lst[I]) then
        Continue;
      cur_folder := LowerCase(ExtractFileName(ExtractFileDir(files_lst[I])));
      if cur_folder <> old_folder then

Open in new window

Author

Commented:
This little project is going to drive me more nuts than my dog!

I started over with a new program.  I'm able to at least get the TabSheets and Captions correctly.  Attempts to add ScrollBox and TImage controls fail.

Next, I added controls to the program to show how I would like it to look when completed.  

Attached is a composite of 3 Screen Shots of the program illustrating my comments above:

1.  Left Image - Program as is. (broken).
2.  Middle - Program showing 1st Tab.
3.  Right - Program showing 2nd Tab

Attached is the "as-is" program.  

Starting with the First tab, my assumption is that when adding a new control (layer) the Parent of the new control must point back to the control it "sits on".  

For example:
1.  The Parent of a TImage is TScrollBox.
2.  The Parent of TScrollBox is TTabSheet, ...etc...,
3.  The parent of Panel1 & Panel2 is the Form.

If this assumption is true, what I don't know is how to assign Parent relationship(s) between two layers of controls.

Can someone please help me out with this?

Thanks!




Gallery3.jpg
Gallery.zip
Commented:
procedure TfGallery.btnBuildLayersClick(Sender: TObject);
var
  i, j, iActLeft, iActTop : integer;
  scrlb: TScrollBox;
begin
  //Create TabSheets & add Captions
  for i := 0 to 4 do begin
    with  TTabSheet.Create(Self) do
    begin
      PageControl := PageControl1;
      PageControl1.ActivePageIndex := i;
      //Caption := arTabCaptions[i];
      case i of
       0 : Caption := 'A - C';
       1 : Caption := 'D - F';
       2 : Caption := 'G - M';
       3 : Caption := 'N - S';
       4 : Caption := 'S - Z';
      end;

      //Add ScrollBox to each TabSheet
      scrlb := TScrollBox.Create(fGallery);
      with scrlb do begin
        PageControl1.ActivePageIndex := i;
        Parent :=PageControl1.ActivePage;
        Name := 'ScrollBox' + IntToStr(i);
        Color := clMedGray;
        Align := alClient;
        Visible := True;
      end;

      //Attempt to place images on each TAbSheet.
      iActTop  := 4;
      iActLeft := 6;
      //Place 3 TImage on each TabSheet
      for j := 1 to 3 do begin
        with TImage.Create(Self) do
         begin
           Parent := scrlb;
           Name   := 'Image' + IntToStr(i) + IntToStr(j);
           Width  := 105;
           Height := 160;
           Top    := iActTop;
           Left   := iActLeft;
           Picture.LoadFromFile('C:\tmp\th_cover.bmp');
           Refresh;
           iActLeft := iActLeft + 125;
        end;
      end;
      iActLeft := 6; //Reset Left Position for new TabSheet.
    end;
  end;
end;

Open in new window


??
Commented:
please also check
....
      PageControl := PageControl1;
      PageControl1.ActivePageIndex := i;
.....

Open in new window

to
......
      PageControl := PageControl1;
      Name := 'TabSheet' + IntToStr(i);
      PageControl1.ActivePageIndex := i;
.......

Open in new window

Author

Commented:
The program is very close.  The ScrollBars are now on each tabsheet.  However,  images are only placed on the last tab.  No images on the first 4 tabs.

I modified the program slightly and remove the first for loop (0..4) around the TImage.Create section.  It seemed redundant since I already have a for (0..4) loop at the start of this section.

Based on the screen shot below, all of the TImages controls are being created.  Are they hidden under other controls?  I added BringToFront in the TImage section:

BringToFront;
Picture.LoadFromFile('C:\tmp\beach-ocean-tree.bmp');
Refresh;
but that did not help.
begin
  for i := 0 to 4 do begin
    with  TTabSheet.Create(Self) do
    begin
      PageControl := PageControl1;
      PageControl1.ActivePageIndex := i;
      PageControl1.ActivePage := TabSheet; ///Is this correct to do?
      Parent := PageControl1; // Is it correct to do this ?
      Visible := true;
      case i of
       0 : Caption := 'A - C';
       1 : Caption := 'D - F';
       2 : Caption := 'G - M';
       3 : Caption := 'N - S';
       4 : Caption := 'S - Z';
      end;

       //Add ScrollBox to each TabSheet
      scrlb := TScrollBox.Create(fGallery);
      with scrlb do begin
        PageControl1.ActivePageIndex := i;
        Parent :=PageControl1.ActivePage;
        Name := 'ScrollBox' + IntToStr(i);
        Color := clMedGray;
        Align := alClient;
        Visible := True;
      end;
    end;
  end;

  iActTop  := 4;
  iActLeft := 6;

   for j := 1 to 3 do begin
       with TImage.Create(Self) do
       begin
           Parent := scrlb;
           Name   := 'Image' + IntToStr(i) + IntToStr(j);
           Width  := 105;
           Height := 160;
           Top    := iActTop;
           Left   := iActLeft;
           Picture.LoadFromFile('C:\tmp\beach-ocean-tree.bmp');
           Refresh;
         iActLeft := iActLeft + 125;
       end;
   end;
   iActLeft := 6;
end;

Open in new window

Gallery6.jpg

Author

Commented:
Please disregard my last post.  The program is working correctly.  I ended the for (0..4) at the top before the TImage section.  Once I moved the "end ;//for 0--4)" to the bottom, it works beautiful!!!

Author

Commented:
Here is a screen shot of the program as it is now!  The screen shot is a composition of the first and last tab.  The 3 tabs in between look exactly the same.

Success!!!!

Thanks very much!!!
Gallery9.jpg
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial