Back

Delphi Text Editor Code!

This web page is to help a beginner build a text editor!

Bold
CurrText.Style := CurrText.Style + [fsBold];
Regular
CurrText.Style := CurrText.Style - [fsBold];
To insert the Date and Time in a Editor:

procedure TForm1.Date1Click(Sender: TObject);
begin
    Editor.SelText := (FormatDateTime('ddd, mmm d, yyyy ', Now));
end;
procedure TForm1.Time1Click(Sender: TObject);
begin
    Editor.SelText := (FormatDateTime('hh:mm AM/PM ', Now));
end;
 

Paste from Clipboard:

procedure TForm1.Paste1Click(Sender: TObject);
begin
    Editor.PasteFromClipboard;
end;
 

Copy to the Clipboard:

procedure TForm1.Copy1Click(Sender: TObject);
begin
    Editor.CopyToClipboard;
end;
 

Cut to the Clipboard:

Editor.CutToClipboard;
 

Editor Alignments. Left - Right - Center.
begin
    Editor.Alignment := taLeftJustify;
end;
begin
    Editor.Alignment := taRightJustify;
end;
begin
    Editor.Alignment := taCenter;
end;
Counting Characters in a text file (Editor).Using a Dialog.
begin
  MessageDlg (Format (
    'The text has %d characters', [Memo1.GetTextLen]),
    mtInformation, [mbOK], 0);
end;
SetFocus:

Editor.SetFocus;
Memo1.SetFocus;
etc...
 

Changing a Editor colour. Using the ColorDialog:

begin
    ColorDialog1.Color := Editor.Color;
  if ColorDialog1.Execute then
    Editor.Color := ColorDialog1.Color;
end;
 

Positioning the Caret in a Editor:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Editor.SetFocus;
    Editor.SelStart  :=  2;
end;

0 is the first character.

Using Quick Reports for a Text Editor Preview (Simple Example).

First drop a TQuickRep on a Form,
Put a DetailBand on it,
Then drop a TQRMemo on the DetailBand.

Form3.QRMemo1.lines := Editor.Lines;
Form3.QuickRep1.Preview;

To get a Text Editor to load a text file when your Text Editor is associated with your systems *.txt files.

procedure TForm1.FormShow(Sender: TObject);
begin
    if (ParamCount > 0) and FileExists(ParamStr(1)) then
      Form1.Memo1.Lines.LoadFromFile(ParamStr(1));
end;

This will put the opened files name on the taskbar button and shows you how to use ExtractFileName .
Application.Title := ExtractFileName(OpenDialog.FileName);
Loading a text file into a TMemo
  1. First start a new project.
  2. Place Memo1 (TMemo) on Form1
  3. Set Memo1 Align to alBottom
  4. Place Button1 (TButton) on Form1 at the top of Form1
  5. Click on Form1 and in the Object Inspector click on Events
  6. DoubleClick on OnCreate
  7. 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.


 

Links:















Back