Board index » delphi » Plz help me!!

Plz help me!!

How to save information into a txt-fil?
Like when the program starts you have to put in your name and when the
program is closing it saves the information and the next time someone
open the program it opens every thing the person wrote!

 

Re:Plz help me!!


Hi,

See AssignFile(), Reset(), Rewrite(), WriteLn(), ReadLn(), CloseFile() in
the online help.  These functions have to do with writing/reading text
files.

Jon.

Quote
east...@swipnet.se wrote:
> How to save information into a txt-fil?
> Like when the program starts you have to put in your name and when the
> program is closing it saves the information and the next time someone
> open the program it opens every thing the person wrote!

Re:Plz help me!!


Hi,

Another option is to use the TIniFile.  See online help for this too.  I
could provide sample code for you, but the online help does too.

Good luck,
Jon.

Quote
east...@swipnet.se wrote:
> How to save information into a txt-fil?
> Like when the program starts you have to put in your name and when the
> program is closing it saves the information and the next time someone
> open the program it opens every thing the person wrote!

Re:Plz help me!!


Better to use the Win95 registry for saving the curent state of an
application.  See online help for TRegistry (Delphi 3.0).

Here is two procedures that saves and loads a forms height and width:

type
  TMyForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure RegistrySave (Key: HKey; const Path: String);
    procedure RegistryLoad (Key: HKey; const Path: String);
  end;

  TMyFormRegData = record
    Height,Width: Integer;
  end;

procedure TMyForm.RegistrySave (Key: HKey; const Path: String);
var
  RegData: TMyFormRegData;
  Registry: TRegistry;
begin
  try
    Registry := TRegistry.Create;
    Registry.RootKey := Key;
    if Registry.OpenKey(Path+'\'+Name,true) then begin
      RegData.Height := Height;
      RegData.Width := Width;
      Registry.WriteBinaryData(Name,RegData,SizeOf(RegData));
    end;
  finally
    Registry.CloseKey;
    Registry.Destroy;
  end;
end;

procedure TMyForm.RegistryLoad (Key: HKey; const Path: String);
var
  RegData: TMyFormRegData;
  Registry: TRegistry;
begin
  try
    Registry := TRegistry.Create;
    Registry.RootKey := Key;
    if Registry.OpenKey(Path+'\'+Name,false) then begin
      Registry.ReadBinaryData(Name,RegData,SizeOf(RegData));
      Height := RegData.Height;
      Width := RegData.Width;
    end;
  finally
    Registry.CloseKey;
    Registry.Destroy;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  RegistrySave(HKEY_CURRENT_USER,'\Software\MyApp\MyForm');
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  RegistryLoad(HKEY_CURRENT_USER,'\Software\MyApp\MyForm');
end;

Geir Wikran

Other Threads