home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0022_Add Controls to TTabbedNotebook.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  4.2 KB  |  118 lines

  1.  
  2. I have seen the question "how do you add controls to TTabbedNotebook
  3. or TNotebook at run-time?" several times here and elsewhere.
  4. Well, after finally getting a few spare minutes to check into
  5. it, I have stumbled across the solution:
  6.  
  7.  
  8. TTabbedNotebook
  9. ---------------
  10. Adding controls to a TTabbedNotebook during design time is
  11. a pretty simple task.  All you need to do is set the PageIndex
  12. or ActivePage property to the page you want to add controls
  13. to, and begin dropping the controls onto the TTabbedNotebook.
  14.  
  15. Adding controls to a TTabbedNotebook during run-time is also
  16. very simple.  However, there is no mention what-so-ever in
  17. the Delphi documentation on how to do this.  To make matters
  18. worse, the TTabbedNotebook source code is not included when
  19. you purchase the Delphi VCL source.  Thus, we are left with
  20. a mystery.  Fortunately, I have stumbled across the solution.
  21.  
  22. The first step to solving this mystery was to take a look
  23. at \DELPHI\DOC\TABNOTBK.INT, the interface section of the
  24. TABNOTBK.PAS unit where TTabbedNotebook is defined.  A quick
  25. examination will reveal the TTabPage class, which is described
  26. as holding the controls for a given page of the TTabbedNotebook.
  27.  
  28. The second clue to solving this case comes from observation
  29. that the Pages property of TTabbedNotebook has a type of TStrings.
  30. It just so happens that Delphi's TStrings and TStringList classes
  31. provide both Strings and Objects property pairs.  In other words,
  32. for every string in TStrings, there is a corresponding Objects
  33. pointer.  In many cases, this extra pointer is ignored, but if
  34. you're like me, you're thinking "Ah-hah!"
  35.  
  36. After a quick little test in code, sure enough, the Objects property
  37. points to a TTabPage instance -- the one that corresponds to the
  38. page name in the Strings property.  Bingo!  Just what we were looking
  39. for.  Now see what we can do:
  40.  
  41. { This procedure adds places a button at a random location on the }
  42. { current page of the given TTabbedNotebook.                      }
  43.  
  44. procedure AddButton(tabNotebook : TTabbedNotebook);
  45. var
  46.   tabpage : TTabPage;
  47.   button  : TButton;
  48. begin
  49.   with tabNotebook do
  50.     tabpage := TTabPage(Pages.Objects[PageIndex]);
  51.   button := TButton.Create(tabpage);
  52.   try
  53.     with button do begin
  54.       Parent := tabpage;
  55.       Left   := Random(tabpage.ClientWidth - Width);
  56.       Top    := Random(tabpage.ClientHeight - Height);
  57.     end;
  58.   except
  59.     button.Free;
  60.   end;
  61. end;
  62.  
  63.  
  64. TNotebook
  65. ---------
  66. The process of adding controls to a TNotebook is almost exactly
  67. the same as that for TTabbedNotebook -- only the page class type
  68. is TPage instead of TTabPage.  However, if you look in
  69. DELPHI\DOC\EXTCTRLS.INT for the type declaration for TPage,
  70. you won't find it.  For some reason, Borland did not include the
  71. TPage definition in the DOC files that shipped with Delphi.
  72. The TPage declaration *IS* in the EXTCTRLS.PAS unit that you
  73. get when you order the VCL source, right where it should be
  74. in the interface section of the unit.  Here's the TPage information
  75. they left out:
  76.  
  77.   TPage = class(TCustomControl)
  78.   private
  79.     procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  80.   protected
  81.     procedure ReadState(Reader: TReader); override;
  82.     procedure Paint; override;
  83.   public
  84.     constructor Create(AOwner: TComponent); override;
  85.   published
  86.     property Caption;
  87.     property Height stored False;
  88.     property TabOrder stored False;
  89.     property Visible stored False;
  90.     property Width stored False;
  91.   end;
  92.  
  93. Now, to make the above procedure work for adding a button to
  94. a TNotebook, all we have to do is replace "TTabbedNotebook" with
  95. "TNotebook" and "TTabPage" with "TPage", as follows:
  96.  
  97. { This procedure adds places a button at a random location on the }
  98. { current page of the given TNotebook.                            }
  99.  
  100. procedure AddButton(Notebook1 : TNotebook);
  101. var
  102.   page    : TPage;
  103.   button  : TButton;
  104. begin
  105.   with Notebook1 do
  106.     page := TPage(Pages.Objects[PageIndex]);
  107.   button := TButton.Create(page);
  108.   try
  109.     with button do begin
  110.       Parent := page;
  111.       Left   := Random(page.ClientWidth - Width);
  112.       Top    := Random(page.ClientHeight - Height);
  113.     end;
  114.   except
  115.     button.Free;
  116.   end;
  117. end;
  118.