article

Delphi Text Editor Code

Email
Submitted on: 1/1/2015 1:04:00 AM
By: Anonymous (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: Delphi 5, Delphi 4, Pre Delphi 4
Views: 5588
 
     Here's all the code to create your own 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.

Download here - Download.


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.