Loading a text file into
a TMemo
- First start a new project.
- Place Memo1 (TMemo) on Form1
- Set Memo1 Align to alBottom
- Place Button1 (TButton) on Form1 at the top
of Form1
- Click on Form1 and in the Object Inspector
click on Events
- DoubleClick on OnCreate
- Type in this code:
begin
Memo1.Lines.LoadFromFile('c:\ssapcs\desktop1\config1.txt');
end;
{ Your text file will be in a directory (Folder)
that you want to load }
Next DoubleClick on Button1
and type this code
begin
Memo1.Clear;
end;
Run Project1 (Or whatever you called it),
now you should be able to see the text from
c:\ssapcs\desktop1\config1.txt in Memo1.
Click on Button1 and Memo1 will clear.
Hows that for easy!
Part 2
Saving the Memo lines to a text file is simple.
Put 3 Buttons on Form1,
keep Memo1 on the Form.
Name the first Button 'Open',
name the second Button 'Save'
and
name the third Button 'Clear'.
Double-Click on the button named 'Open',
write this code between 'begin' and 'end'
procedure TForm1.OpenClick(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('c:\ssapcs\desktop1\config1.txt');
Save.Enabled := True;
end;
Double-Click on the
button named 'Save',
write this code between 'begin' and 'end'
procedure TForm1.SaveClick(Sender: TObject);
begin
Memo1.Lines.SaveToFile('c:\ssapcs\desktop1\config1.txt');
end;
Double-Click on the
button named 'Clear',
write this code between 'begin' and 'end'
procedure TForm1.ClearClick(Sender: TObject);
begin
Memo1.Clear;
end;
Note the names of the
procedures.
In the Object Inspector set the 'Save'
Button to Enabled := False;
Make sure you use the path to a file that
exists and you don't mind editing this file.
Run the program, click on the open button add some text to the Memo
then click on the save button, click on the clear button and open the file
again.
Part 3
A Simple Text
Editor Here is a simple text
editor that uses a Memo and four buttons, the OpenDialog, and the
SaveDialog.
This was made with Delphi 3,
with a few small changes it should work on all versions of Delphi.
Study it carefully, this program will open a file,
then you can edit the file then save the file or clear the memo and start
again. Also a Close button is included to close the program.
If you want to add to this text editor program have
a look at our Delphi Tips web page for some code and ideas.
Download here - Download.
|