Adding a Menu

In this chapter, you will add a menu to the text processor to enable you to call the Text Control's built-in dialog boxes.

Use the Delphi Menu Component to create a Format menu with the items Character... and Paragraph.... (Please refer to the Delphi documentation if you need help with creating menus).

Add the following code to the Click procedures of the menu items:

procedure TForm1.Character1Click(Sender: TObject);
begin
   TXTextControl1.FontDialog
end;
procedure TForm1.Paragraph1Click(Sender: TObject);
begin
   TXTextControl1.ParagraphDialog;
end;

Start the program again. You should be able to use the menu items to call the Font and Paragraph dialog boxes.

Now for the Edit menu. Again use the Menu Design Window and create an 'Edit' menu containing items for Cut, Copy and Paste. The code for these menu items is:

procedure TForm1.Cut1Click(Sender: TObject);
begin
   TXTextControl1.Clip (1);
end;
procedure TForm1.Copy1Click(Sender: TObject);
begin
   TXTextControl1.Clip (2);
end;
procedure TForm1.Paste1Click(Sender: TObject);
begin
   TXTextControl1.Clip (3);
end;

Having added these menu items, you can exchange formatted text with other word processors via the clipboard.

The last menu for now shall be a simple file menu. Create a File menu including the items Load... and Save As.... Place a common dialog box icon on the form and enter the following code, which will call the common dialog box to get a file name from the user, and will then load respectively save the selected file:

procedure TForm1.Load1Click(Sender: TObject);
const
   TXT_FIlE = 1;
   TXM_FILE = 3;
begin
   OpenDialog1.Title := 'Open file';
   OpenDialog1.Filename := '';
   OpenDialog1.Filter :=
      'Text Control Demo (*.txm)|*.txm
         |Plain text(*.txt)|*.txt';
   OpenDialog1.FilterIndex := 1;
   If OpenDialog1.Execute then begin;
   // Pass the filename to the text control
   If UpperCase(copy(OpenDialog1.Filename,
      length(OpenDialog1.filename)-2, 3)) = 'TXM' then begin
      TXTextControl1.Load(OpenDialog1.Filename, 0,
         TXM_FILE, 0);
   end
   else
      TXTextControl1.Load(OpenDialog1.Filename, 0,
         TXT_FILE, 0);
   end;
end;
procedure TForm1.Saveas1Click(Sender: TObject);
const
   TXM_FILE = 3;
begin
   SaveDialog1.Title := 'Save as ...';
   SaveDialog1.Filename := '';
   SaveDialog1.Filter := 'Text Control Demo (*.txm)|*.txm';
   SaveDialog1.FilterIndex := 1;
   SaveDialog1.DefaultExt := 'txm';
   if SaveDialog1.Execute then begin;
      // Pass the filename to the text control
      TXTextControl1.Save(SaveDialog1.Filename, 0,
         TXM_FILE, 0);
   end;
end;