Important alert: (current site time 7/16/2013 4:41:15 AM EDT)
 

article

Delphi Text Editor Code

Email
Submitted on: 7/9/2000 6:06:44 PM
By:  
Level: Intermediate
User Rating: By 8 Users
Compatibility: Delphi 5, Delphi 4, Pre Delphi 4
Views: 64230
 
     Here's all the code to create your own editor!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

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 31 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

2/19/2001 12:27:44 AMMike Tomte

Good code. I used it as the basic framework for PHP Magic =) I rate it excellent, as it help newbies like me.
(If this comment was disrespectful, please report it.)

 
1/13/2003 6:56:47 PM

Do you send this code to me? (i can't load)
(If this comment was disrespectful, please report it.)

 
1/27/2003 1:54:07 AM

This link url's is not running this page cannot find on xoom.Please upload this code.
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=64&lngWId=7
(If this comment was disrespectful, please report it.)

 
6/21/2003 3:38:59 PMScott Armstrong

Great code. There's stuff in there that I didn't even know a text editor could have.
(If this comment was disrespectful, please report it.)

 
10/14/2005 7:56:54 AMRobin

Hi good code dude, but im looking for the command that can remove spaces in edit boxes, u cant help by any chance?
(If this comment was disrespectful, please report it.)

 
4/30/2008 10:13:14 AMRussCA

I can't find the download for the code. When I click on "Download" I am taken to a page offering various Web Master items.
(If this comment was disrespectful, please report it.)

 

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.