home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0020_Add-Delete pages - tabbed notebook.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  2.5 KB  |  56 lines

  1.  
  2. procedure AddPage(nbk : TNotebook; tabset : TTabSet; const pagename : string);
  3. { Adds a new page to nbk and a new tab to tabset with pagename for the text,   }
  4. { places a memo on the page, and brings the new page to the top.               }
  5. { Assumes the tabset has exactly one tab for each notebook page in same order. }
  6. var
  7.   memo : TMemo;
  8.   page : TPage;
  9. begin
  10.   if nbk <> nil then begin
  11.     nbk.Pages.Add(pagename);                       {add a page to the TNotebook}
  12.     nbk.PageIndex := nbk.Pages.Count - 1;       {make new page the current page}
  13.     if tabset <> nil then begin
  14.       tabset.Tabs.Add(pagename);                            {add a matching tab}
  15.       tabset.TabIndex := nbk.PageIndex;           {make new tab the current tab}
  16.     end;
  17.     if nbk.PageIndex > -1 then begin                   {make sure a page exists}
  18.       page := TPage(nbk.Pages.Objects[nbk.PageIndex]);         {get page object}
  19.       memo := TMemo.Create(page);                  {create memo (owned by page)}
  20.       try
  21.         memo.Parent := page;                                {set page as Parent}
  22.         memo.Align  := alClient;             {set alignment to fill client area}
  23.       except
  24.         memo.Free;                           {free memo if something goes wrong}
  25.       end;
  26.       page.Visible := true;                  {make sure the page gets displayed}
  27.     end;
  28.   end;
  29. end;
  30.  
  31. procedure DeletePage(nbk : TNotebook; tabset : TTabSet; index : integer);
  32. { Deletes the page whose PageIndex = index from nbk and tabset. }
  33. { Assumes the tabset has exactly one tab for each notebook page in same order. }
  34. var
  35.   switchto : integer;
  36. begin
  37.   if nbk <> nil then begin
  38.     if (index >= 0) and (index < nbk.Pages.Count) then begin
  39.       if index = nbk.PageIndex then begin
  40.         if index < nbk.Pages.Count - 1 then begin  {if page is not last in list}
  41.           switchto := nbk.PageIndex;     {show page behind current after delete}
  42.           if (index = 0) and (nbk.Pages.Count > 1) then          {if first page}
  43.             nbk.PageIndex := 1;                           {show second page now}
  44.         end else
  45.           switchto := nbk.PageIndex - 1;        {else, show page before current}
  46.       end;
  47.       nbk.Pages.Delete(index);          {free's the page & all controls it owns}
  48.       if tabset <> nil then begin
  49.         if index < tabset.Tabs.Count then
  50.           tabset.Tabs.Delete(index);                  {delete corresponding tab}
  51.       end;
  52.       nbk.PageIndex := switchto;
  53.     end;
  54.   end;
  55. end;
  56.