This a step-by-step guide to create a simple text editor in Delphi. In this example you will get to know some more components.
First, make a new application by choosing File - New Application. You will see a blank form and an almost empty source code window. As a first step create the main menu of the editor. To do this double-click on the MainMenu component form the "standard" tab. Now you have an blank, useless menu. To add any contents to it double-click on its icon. Delphi will open the menu editor. All you will see is an blue box in the top left corner. This is the first menu entry. To give it a name, go to the object inspector and change the "caption" property to "&File". Please note that the "&" is no typo but underline the character after it. This character can be used as a hotkey in your program. Click in the menu editor outside the blue box. The entry will be renamed to "File". Now you have an empty menu under it. Rename the first entry to "&Exit". Double-click on Exit to open the code that is executed when the user clicks on the menu-entry. Put "Close;" here. Press F9 to compile your program.
Now you can add more entries to the menu by using the above procedure. Name them "&New", "&Open", "&Save", "Save &As", and "&Print". You can put seperators between them by using "-" as caption.
Add a RichEdit component (Win32 palette) to your form. RichEdit is a component that allows the user to input multi-line text. He can also change attributes (such as bold, italic...) but we will not use this. The advantage of RichEdit over Memo is that RichEdit automatically provides scrollbars. Set the "Align" property of the RichEdit to alClient. This will ensure that it is always as big as the form. Next select the lines property. You will see a button with three dots. Click it to go to the line editor. Delete "RichEdit1".
To allow the user to select a file to open and a filename to save add a OpenDialog and a SaveDialog (Dialogs palette) to your form. Opening a file to the RichEdit is fairly easy. Go to the menu editor (double-click on the menu icon on your form), then double-click on open. To open a file into the RichEdit add the following code:
procedure TForm1.Open1Click(Sender: TObject); begin if opendialog1.execute then RichEdit1.Lines.LoadFromFile(Opendialog1.filename); end;
Execute the program and try to load a file. If it is longer than one screen you will see that you don't get the scrollbars you expected. Exit the program, select the RichEdit, and look at the object inspector. You will find a property ScrollBars. It is set to ssNone by default. Set it to ssBoth. Start the program again. Now the RichEdit has always scrollbars which is not quite what we want. To hide the scrollbars if they are not needed set HideScrollBars to true.
As a next step we will add a filename and changed variable to the form. Filename is (obviously) there to contain the filename of the file that is currently edited. Changed is set to true if the user changed the file. Add the open procedure to:
procedure TForm1.Open1Click(Sender: TObject); begin if opendialog1.execute then begin RichEdit1.Lines.LoadFromFile(Opendialog1.filename); filename:= OpenDialog1.filename; changed:= false; end; end;
And the TForm1 class to:
TForm1 = class(TForm) {components left out} procedure Exit1Click(Sender: TObject); procedure Open1Click(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } filename: String; changed: boolean; end;
To trigger any changes to the RichEdit component you can use its OnChange event. Double-click on it to edit its source. Since the variable changed is declared in the same object as this procedure we can simply put:
procedure TForm1.RichEdit1Change(Sender: TObject); begin changed:= true; end;
This will set changed to true every time the user changes anything. Next we will implement the save-feature. Go to the menu editor to edit the source associated with the "&Save" entry in the menu. The lines property of TRichEdit is a TStrings. This class has a lot of capabilities, including loading and saveing to and from files (look at the help file form more information).
procedure TForm1.Save1Click(Sender: TObject); begin if (filename<>'') and (changed= true) then begin RichEdit1.Lines.SaveToFile(Filename); changed:= false; filename := ''; end; end;
Now the user can save the changes he made to the file. He still cannot save the file by a different name. Change the source:
procedure TForm1.SaveAs1Click(Sender: TObject); begin if SaveDialog1.Execute then begin RichEdit1.Lines.SaveToFile(SaveDialog1.Filename); filename:= SaveDialog1.Filename); changed:= false; end; end;
You will probably notice that there are no file-types in the SaveDialog (there are none in the OpenDialog eiter). To change this, select the dialog and edit the filter property.
Implementing the new-function is faily easy. All you have to do is ask the user if he wants to save (only when the file was changed) and then to clear the RichEdit.
procedure TForm1.New1Click(Sender: TObject); begin if changed= true then begin if MessageDlg('Do you want to save the current file?', mtWarning,[mbYes, mbNo],0)= idYes then Save1Click(self); RichEdit1.Clear; end else RichEdit1.clear; end;
Next is the printing procedure. Printing is very easy - thanks to RichEdit. Just call the TRichEdit.Print method. You can even pass a caption for the print job. We will use the filename for this. Printing prints to the standard printer.
procedure TForm1.Print1Click(Sender: TObject); begin Richedit1.print(filename); end;
Lets provide the standard editing functions next. Make a new top-level menu called "&Edit" with submenus "Cu&t", "&Copy", "&Paste" and "C&lear". All these functions are very easy to implement because TRichEdit provides all of them. All you have to do is connect the RichEdit functions with your menu:
procedure TForm1.Cut1Click(Sender: TObject); begin RichEdit1.CutToClipboard; end; procedure TForm1.Copy1Click(Sender: TObject); begin RichEdit1.CopyToClipboard; end; procedure TForm1.Paste1Click(Sender: TObject); begin RichEdit1.PasteFromClipboard; end; procedure TForm1.Clear1Click(Sender: TObject); begin RichEdit1.Clear; end;
Now we are about as good as the standard Windows notepad. Didn't take very long, huh?
Let's add a couple of additional features to the editor. First a "&Options" menu with a "&Font" submenu. Luckily we don't need to write the dialog to choose the font. Delphi/Windows provides it for us. The component is located in the "dialogs" palette. The source to use it looks like this:
procedure TForm1.Font1Click(Sender: TObject); begin if FontDialog1.Execute then RichEdit1.Font:= FontDialog1.Font; end;Fifth Day Seventh Day
Please e-mail me with any comments!
© 27.12.96 Sebastian Boßung
Home