Saving the Controls

Saving a document which has been created with this program necessitates storing not only the data contained in the Text Controls, but also the number and the positions of the controls. In addition, a format identifier should be stored to enable the load routine of the program to determine if it can process a file which it is about to load. The code on the following page shows you how to save the document.

procedure TForm1.Save1Click(Sender: TObject);
var
   outFile : file of byte;
begin
   // Create an "Open File" dialog box
   SaveDialog1.Title := 'Save as';
   SaveDialog1.Filename := '';
   SaveDialog1.DefaultExt := 'txm';
   SaveDialog1.Filter := 'TX Form Demo (*.txf)|*.txf';
   SaveDialog1.FilterIndex := 1;
   if SaveDialog1.Execute then
      Filename := SaveDialog1.Filename
   else
      Exit;
   try
      begin
      // Open the selected file
      AssignFile(outFile, filename);
      rewrite(outFile);
      // Write file header
      FileID.lVersion := File_ID;
      BlockWrite(outFile, FileID, sizeOf(FileID));
      //Save properties of all text controls
      BlockWrite(outFile, MaxID, sizeof(MaxID));
      For i := 1 To MaxID do begin
         TXhWnd := FindComponent('TXTextControl'
               + Inttostr(i));
         with TXProp do begin
            xPos := TTXTextControl(TXhWnd).Left;
            yPos := TTXTextControl(TXhWnd).Top;
            xSize := TTXTextControl(TXhWnd).Width;
            ySize := TTXTextControl(TXhWnd).Height;
         end;
         BlockWrite(outFile, TXProp, sizeof(TXProp));
      end;
      closeFile(outFile);
      //Save contents of all text controls
      For i := 1 to MaxID do begin
         TXhWnd := FindComponent('TXTextControl'
               + InttoStr(i));
         TTXTextControl(TXhWnd).Save(FileName, -1,
               3, False);
      end;
   end;
   Except
      MessageDlg('Error saving ' + filename, mtError,
            [mbOK], 0);
   end;
end;

The Load routine first reads the format ID and the number of controls. Then it creates the required number of controls, loads their contents and finally moves them to their correct position:

procedure TForm1.Load1Click(Sender: TObject);
var
   lFilePos : LongInt;
   InpFile  : file of byte;
   bOpen : Boolean;
begin
   // Create an "Open File" dialog box
   OpenFileDialog.Title := 'Open file';
   OpenFileDialog.Filename := '';
   OpenFileDialog.Filter := 'TX Form Demo (*.txf)|*.txf';
   OpenFileDialog.FilterIndex := 1;
   if OpenFileDialog.Execute then
      Filename := OpenFileDialog.Filename
   else Exit;
   try
      begin
         // Open the selected file
         bOpen := False;
         AssignFile(inpFile, filename);
         reset(inpFile);
         bOpen := True;
         // Read file header
         BlockRead(inpFile, FileID, sizeOf(FileID));
         If FileID.lVersion <> FILE_ID Then begin
            MessageDlg('Wrong filetype: ' + filename,
               mtError,[mbOK],0);
            Exit;
         End;
         // Destroy existing controls
         If MaxID > 1 then begin
            For i := 2 To MaxID do begin
               TXhWnd := FindComponent('TXTextControl'
                  + Inttostr(i));
               TTXTextControl(TXhWnd).Free;
            end;
         end;
         //Create text controls and load their contents
         BlockRead(InpFile, MaxID, sizeof(MaxID));
         For i := 1 To MaxID do begin
            BlockRead(InpFile, TXProp, sizeof(TXProp));
            If i <> 1 Then begin
               TX := TTXTextControl.Create(Form1);
               TX.Parent := Form1;
               TX.Width := TXTextControl1.Width;
               TX.Height := TXTextControl1.Height;
               TX.Name := 'TXTextControl'
                  + InttoStr(i);
               TX.SizeMode := 3;
               TX.BringtoFront;
               TX.OnMouseDown := TXTextControl1MouseDown;
            end;
            TXhWnd := FindComponent('TXTextControl'
               + Inttostr(i));
            TTXTextControl(TXhWnd).Left := TXProp.xPos;
            TTXTextControl(TXhWnd).Top := TXProp.yPos;
            TTXTextControl(TXhWnd).Width := TXProp.xSize;
            TTXTextControl(TXhWnd).Height := TXProp.ySize;
            TTXTextControl(TXhWnd).Text := '';
         end;
         lFilePos := FilePos(InpFile);
         closeFile(InpFile);
         bOpen := False;
         For i := 1 To MaxID do begin
            TXhWnd := FindComponent('TXTextControl'
               + Inttostr(i));
            lFilePos := TTXTextControl(TXhWnd).Load(
               FileName, lFilePos, 3, False);
            TTXTextControl(TXhWnd).Visible := True;
         end;
      end;
   Except
      MessageDlg('Error opening ' + filename, mtError,
            [mbOK], 0);
      if bOpen then begin
         closeFile(inpFile);
      end;
   end;
end;