Creating A Text Editor in Delphi, A Tutorial
Creating A Text Editor in Delphi, A Tutorial
Next to the form, you’ll see the Object Inspector, which you can use to set property
values for the form and components you place on it.
To start designing the text editor, add a RichEdit and a StatusBar component to the
form:
1 To create a text area, drop a RichEdit component onto the form.
Click the Win32 page on the Component palette. To find the RichEdit component,
point to an icon on the palette for a moment; Delphi displays a Help hint showing
the name of the component.
When you find the RichEdit component, double-click it to place it on the form.
To do this, click on RichEdit1 to select it on the form, then choose the Align
property in the Object Inspector. Select alClient from the drop-down list.
The RichEdit component now fills the form so you have a large text editing area.
By choosing the alClient value for the Align property, the size of the RichEdit
control will vary to fill whatever size window is displayed even if the form is
resized.
3 Double-click the StatusBar component on the Win32 page of the Component
palette. This adds a status bar to the bottom of the form.
Next we want to create a place to display the name of the file being edited. You
can do this in two ways. The easiest way is to set the SimplePanel property of the
StatusBar1 object to True and assign any text that you want to display to the
SimpleText property. This provides only one panel in the status bar. You can assign
its value as follows:
StatusBar1.SimpleText := ’untitled.txt’;
However, many times you will want to include more than one panel in the status
bar so you can include more than one piece of information. You can specify more
than one panel by using the Panels property of the TStatusBar component as
explained in the next few steps.
4 Double-click the status bar to display the Editing StatusBar1.Panels dialog box.
5 Right-click on the dialog box and choose Add to add the panel to the status bar.
The Panels property is a zero-based array that allows you to access each Panel that
you create based on its unique index value (by default, it is 0 for this panel). Use
the default property values for the panel. Click the X in the upper right corner to
close the dialog box. Now the main editing area of the user interface for the text
editor is set up.
Following are the kinds of actions our sample text editor application needs:
You can also centralize images to use for your toolbar and menus in an ImageList.
To add an ActionList and an ImageList to your form:
1 From the Standard page of the Component palette, drop an ActionList component
onto the form. The ActionList component is nonvisual, so it doesn’t matter where
you put it on the form. It won’t appear at runtime.
2 From the Win32 page, choose the ImageList component and drop it onto your form.
It’s also nonvisual so you can put it anywhere.
Your form should now resemble the following figure.
The ActionList
and ImageList
objects don’t
show when the
application is
running.
Editing area
Status bar
3 In the Object Inspector, set the following properties for the action:
• After Caption, type &New. Note that typing an ampersand before one of the letters
makes that letter a shortcut to accessing the command.
• After Category, type File. This organizes the File commands in one place.
• After Hint, type Create file (this will be the Help hint).
• After ImageIndex, type 0 (this will associate image number 0 in your ImageList
with this action).
• After Name, type FileNew (for the File|New command).
2 Double-click TEditCut. The action is created along with a new category called
Edit. EditCut1 should be selected.
3 In the Object Inspector, set the following property for EditCut1:
• After ImageIndex, type 4.
The other properties are set automatically.
4 Right-click on the Action List editor and choose New Standard Action.
5 Double-click TEditCopy.
6 In the Object Inspector, set the following properties:
• After ImageIndex, type 5.
7 Right-click on the Action List editor and choose New Standard Action.
8 Double-click TEditPaste.
9 In the Object Inspector, set the following properties:
• After ImageIndex, type 6.
10 Now you’ve got all the actions that you’ll need for the menus and toolbar. If you
click on the category All Actions, you can see all the actions in the list:
12 With the Action List still selected on the form, set its Images property to
ImageList1.
Click on the Images property, then on the down arrow next to Images.
ImageList1 is listed for you. Select it. This associates the images that we’ll add
to the image list with the actions in the action list.
• Click Add and select filesave.bmp. Delete the grayed out image.
• Click Add and select doorshut.bmp. Delete the grayed out image.
• Click Add and select cut.bmp. Delete the grayed out image.
• Click Add and select copy.bmp. Delete the grayed out image.
• Click Add and select paste.bmp. Delete the grayed out image.
• Click Add and select help.bmp. Delete the grayed out image.
5 Click OK to close the Image List editor.
You’ve added 8 images to the image list and they’re numbered 0-7 consistent with
the ImageIndex numbers on each of the actions.
6 To see the associated icons on the action list, double-click the ActionList object
then select the All Actions category.
When you’re done close the Action List editor. Now you’re ready to add the menu
and toolbar.
Adding a menu
In this section, you’ll add a main menu bar with three drop-down menus—File, Edit,
and Help—and you’ll add menu items to each one using the actions in the action list.
1 From the Standard page of the Component palette, drop a MainMenu component
onto the form. It doesn’t matter where you place it.
2 Set the main menu’s Images property to ImageList1. This will allow you to add the
images to the menu items.
4 In the Object Inspector, type &File to set the Caption property of the first top-level
menu item and press Enter.
5 In the Menu Designer, select the File item you just created. You’ll notice an empty
item under it: select the empty item. In the Object Inspector, choose the Action
property. The Actions from the action list are all listed there. Select FileNew.
• Focus on the item under New and choose FileOpen from its Action property.
• Focus on the item under Open and choose FileSave from its Action property.
• Focus on the item under Save and choose FileSaveAs from its Action property.
When you run your project, Delphi opens the program in a window like the one
you designed on the form. The program is a full-fledged Windows application,
complete with Minimize, Maximize, and Close buttons and a Control menu. The
menus all work although most of the commands are grayed out. The images are
displayed next to menu items with which we associated icons.
Though your program already has a great deal of functionality, there’s still more
to do to activate the commands. And we want to add a toolbar to provide easy
access to the commands.
11 Click the X in the upper right corner to close the application and return to the
design-time view of the form.
Adding a toolbar
Since we’ve set up actions in an action list, we can add some of the same actions that
were used on the menus onto a toolbar.
3 Assign actions from the action list to the first set of buttons.
• Select the first button and set its Action to FileExit.
• Select the second button and set its Action to FileNew.
• Select the third button and set its Action to FileOpen.
• Select the fourth button and set its Action to FileSave.
4 Assign actions to the second set of buttons.
• Select the first button and set its Action to EditCut1.
• Select the second button and set its Action to EditCopy1.
• Select the third button and set its Action to EditPaste1.
5 Assign an action to the last button.
• Select the last button and set its Action to HelpContents.
6 Press F9 to compile and run the project.
Your text editor already has lots of functionality. You can type in the text area.
Check out the toolbar. If you select text in the text area, the Cut, Copy, and Paste
buttons work.
6 Right where the cursor is positioned in the text editor (between begin and end), type
the following lines:
RichEdit1.Clear;
FileName := ’Untitled.txt’;
StatusBar1.Panels[0].Text := FileName;
Your event handler should look like this when you’re done:
4 The Action List editor should still be displayed. If it’s not, double-click the
ActionList icon on the form.
5 In the Action List editor, double-click the FileOpen action.
The Code editor opens with the cursor inside the event handler.
6 Right where the cursor is positioned in the text editor (between begin and end), type
the following lines:
if OpenDialog1.Execute then begin
RichEdit1.Lines.LoadFromFile(OpenDialog1.FileName);
FileName := OpenDialog1.FileName;
Note The Action List editor should still be displayed. If it’s not, double-click the
ActionList icon on the form.
3 In the Action List editor, double-click the FileSaveAs action.
The Code editor opens with the cursor inside the event handler.
4 Right where the cursor is positioned in the text editor, type the following lines:
SaveDialog1.FileName := FileName;
SaveDialog1.InitialDir := ExtractFilePath(Filename);
if SaveDialog1.Execute then begin
RichEdit1.Lines.SaveToFile(SaveDialog1.FileName);
FileName := SaveDialog1.FileName;
StatusBar1.Panels[0].Text := FileName;
end;
Most of the buttons and toolbar buttons work but we’re not finished yet.
To return to design mode, close the Text Editor application by choosing File|Exit, by
clicking the Exit application button on the toolbar of your application, or by clicking
the X in the upper right corner.
If you receive any error messages, click on them to locate the error. Make sure you’ve
followed the steps as described in the tutorial.
Tip To get Help on the HelpCommand method, put the cursor next to HelpCommand
in the editor and press F1.
That’s it for the Help|Contents command.
3 Select the following TLabel items in the About box and change them in the Object
Inspector:
• Change Product Name to Text Editor.
• Make it Version 1.0.
• Enter the year next to Copyright.
4 Select the form itself and change its Caption in the Object Inspector to About Text
Editor.
Tip The easiest way to select the form is to click on the grid portion.
5 Save the About box form by choosing File|Save As and saving it as About.pas.
6 In the Delphi editor, you should have two files displayed: Unit1 and About. Click
on the Unit1 tab to display Unit1.pas.
Click on the tab to display a file associated with a unit. If you open
other files while working on a project, additional tabs appear on the
editor.
When you create a new form for your application, you need to add it to
the uses clause of the main form. Here we’re adding the About box.
8 On the action list, double-click the HelpAbout action to create an event handler.
9 Right where the cursor is positioned in the text editor, type the following line:
AboutBox.ShowModal;
This code opens the About box when the user clicks Help|About. ShowModal
opens the form in a modal state. That means the user can’t do anything until the
form is closed.
3 In the Events tab, double-click OnCreate to create an event handler that describes
what happens when the form is created (that is, when you open the application).
4 Right where the cursor is positioned in the text editor, type the following lines:
Application.HelpFile := ExtractFilePath(Application.ExeName) + ’TextEditor.hlp’;
FileName := ’Untitled.txt’;
StatusBar1.Panels[0].Text := FileName;
RichEdit1.Clear;
This code initializes the application by associating a Help file, setting the value of
FileName to untitled.txt, putting the filename into the status bar, and clearing out
the text editing area.
5 Put the .HLP file and the CNT file into the project application directory (called
projects\TextEditor).
Note If you decided not to investigate how to create a Help file or use the sample one
provided on the web, the application still works but you’ll receive an error
message when you choose either of the Help commands or click Help on the
toolbar.
6 Press F9 to run the application.
You can test the Text Editor now to make sure it works. If errors occur, click on the
error message and you’ll go right to the place in the code where the error occurred.
Congratulations! You’re done.