Text Files

Example One - A File Menu

In this example the form has a Main Menu, an ActionList, an OpenFile dialogue and a SaveFile dialogue:

The ActionList has had two standard action items added:

A File menu item has been created with two options, Open and Save As:

The actions for the options in the menu creation tool are assigned by selecting them and then choosing from the list displayed in the Action property in the Object Inspector:

When the menu editor is closed you can select the items you have created and click on them to activate the code editor and add their handlers.

The File Open and Save As options shown here are standard actions and equivalent to adding a File Open dialogue from the Dialogues palette. Notice that the Open action has the ShortCut property set to 'Ctrl+O', which was done automatically by the Action Manager, while the Save As action has no ShortCut, though one could be added from the list.

The code for opening a text file and copying it to the memo is as follows:

procedure TForm1.Open2Click(Sender: TObject);
var F: textfile;  S:string;
begin
 if OpenDialog1.execute then
  begin
   AssignFile(F, OpenDialog1.FileName);
   Reset(F);
   While not eof (F) do
    begin
     Readln(F, S);
     Memo1.Lines.Add(S);
    end;
   CloseFile(F);
  end;
end;

The code to save a text file is as follows:

procedure TForm1.SaveAs1Click(Sender: TObject);
var F: textfile;  S:string;
var i: integer;
begin
 if SaveDialog1.execute then
  begin
   AssignFile(F, SaveDialog1.FileName);
   Rewrite(F);
   for i:= 0 to memo1.Lines.Count do
    begin
     S:=Memo1.Lines[i];
     Writeln(F, S);
    end;
   CloseFile(F);
  end;
end;

You can now open text files, edit them and save them.

Example Two - Reading Data From a File into a ComboBox

Rather than setting up the items in a ComboBox by typing them into the Items list it is possible to read them from a file or a database table. The following example reads one field of a data file into a ComboBox and displays a text file in a memo on the same form. 

The structure of the data file is as follows:

type
  Tstar=record
   name:string[20];
   group_solo:string[8];
   act_type:string[10];
   nationality:string[10];
   datafile:string[15];
  end;

The contents of a file are read when the form is activated and the first field, the artist's name, is displayed in the ComboBox:

procedure TForm1.FormActivate(Sender: TObject);
begin
 combobox1.clear;
 assignfile(starfile,'starfile.dat');
 reset(starfile);
 while not eof(starfile) do
  begin
   read(starfile, starrecord);
   combobox1.items.add(starrecord.name);
  end;
end;

When the user chooses a name from the list the ComboBox tries to match it. The code then tries to opens the file from the name stored in the data file. If the file name is valid the file will be displayed in the memo. 

The code below opens the text file that corresponds to the name chosen from the combo box.

procedure TForm1.ComboBox1Click(Sender: TObject);
var
 actfile: textfile;
 actname, actfilename, textline: string;
 filepointer:integer;
begin
 actname:=ComboBox1.Text;
 assignfile(starfile,'starfile.dat');
 reset(starfile);
 while not eof(starfile) do
  begin
   read(starfile, starrecord);
   if actname=starrecord.name
   then actfilename:= starrecord.datafile;
  end;
 memo1.Clear;
 assignfile(actfile, actfilename);
 reset(actfile);
 while not eof (actfile) do
  begin
   readln(actfile, textline);
   memo1.Lines.Add(textline);
  end;
end;

Further information on setting up a StringGrid to create and change the file for this application can be found on the StringGrid page.

Back to Tutorial

Back to Palette List