home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue151 / delphi / SaveForm2Beta / sf2beta.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-01-17  |  1.6 KB  |  88 lines

  1. unit sf2beta;
  2.  
  3. { This tries to load an entire form all in one go. It seems to work fine.
  4.   It loads the form and all its components. However, there is a hidden
  5.   problem. Try saving the form. Then, using the Delphi form designer, move
  6.   one of the buttons. Recompile and load your saved form.
  7.   Oops!
  8. }
  9. interface
  10.  
  11. uses
  12.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  13.   StdCtrls;
  14.  
  15. type
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Button2: TButton;
  19.     Edit1: TEdit;
  20.     Button3: TButton;
  21.     Label1: TLabel;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.     procedure Button3Click(Sender: TObject);
  25.  
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.     procedure ZapComponents;
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38.  
  39.  
  40. {$R *.DFM}
  41.  
  42.  
  43. procedure TForm1.ZapComponents;
  44. var
  45.    i : integer;
  46. begin
  47.    for i := 0 to (ComponentCount - 1) do
  48.       Form1.RemoveComponent(Components[0]);
  49. end;
  50.  
  51.  
  52. procedure TForm1.Button1Click(Sender: TObject);
  53. var
  54.   fs : TFileStream;
  55. begin
  56.   fs := TFileStream.Create( 'xxx.txt', fmCreate );
  57.   try
  58.     fs.WriteComponent(Form1);
  59.     label1.Caption := 'Saved';
  60.   finally
  61.     fs.Free;
  62.   end;
  63. end;
  64.  
  65.  
  66. procedure TForm1.Button2Click(Sender: TObject);
  67. var
  68.   fs : TFileStream;
  69. begin
  70.   fs := TFileStream.Create( 'xxx.txt', fmOpenRead );
  71.   ZapComponents;
  72.   try
  73.      fs.ReadComponent(Form1);
  74.      label1.Caption := 'Loaded';
  75.   finally
  76.     fs.Free;
  77.   end;
  78. end;
  79.  
  80. procedure TForm1.Button3Click(Sender: TObject);
  81. begin
  82.   Caption := Edit1.Text;
  83. end;
  84.  
  85. initialization
  86.  
  87. end.
  88.