1. Technology
Send to a Friend via Email

MENUS
Adding menu support to MyNotepad. Part 3.

In this article, we will see how to extend our MyNotepad project that we have been dealing with in the past two articles. Designing the right kind of menus will make your application much more easily to work with. The truth is that user friendly Windows application would not be user friendly without menus.

   Menus in Delphi  TMainMenu
To add a main menu to our project, we first need to add a menu component on a form. To place a TMainMenu component, just double-click on the component on the Standard tab of the Component palette. A MainMenu component creates a menu that's attached to the form's title bar.

Note: Besides TMainMenu component there is also a TPopupMenu component that let's us create a menu that appears (pops-up) when the user clicks the right mouse button in a form or some component when that component has been assigned to the PopupMenu of a component (or form).

   Adding menu support to MyNotepad
It's time to add a main menu to our MyNotepad project. To do this we first have to start Delphi and open our MyNotepad project (we were developing MyNotepad in the previous two articles). Now, place a TMainMenu component on the Form1 as described above.
What we want to achieve here is to provide the user the ability to open, save, search, and replace the text displayed in Memo1 using menu items, not buttons.

   Working with the Menu Designer
Delphi makes creating menus easy with the Menu Designer. We can simply add menu items directly into the Menu Designer window. We can add, delete and rearrange menu items at design time and we do not have to run the program to see the results.
To actually open a Menu Designer we have to double-click the TMainMenu component that is on the form. Delphi that displays the Menu Designer. The menu Designer gives us a sense of what our menus will look like.

Menu Designer

When Menu Designer appears, the first (blank) menu item is highlighted in the Designer, and the Caption property is highlighted in the Object Inspector. Change the Name property to mnuFile, click on the Caption property in the Object Inspector, type &File, and press Enter. A TMenuItem object is created with the Name property set to mnuFile. File menu item is placed in the main menu bar. To add a submenu just type mnuOpen in the name property and Open in the caption property (over the blank item).

Following these steps create menu items for Open (Caption: &Open, Name: mnuOpen) and Save (Caption: &Save, Name: mnuSave) menu items as submenus in the File menu. Finishing this, create Find (Caption: &Find, Name: mnuFind) and Replace (Caption: &Replace, Name: mnuReplace) menu items as submenus in Edit (mnuEdit) menu.

File menu Edit Menu

   OnClick
Okay, so we have all these menu items but no code to make them work. Remember: we want to get rid of buttons. Menu items only trigger an event when the user clicks on them (OnClick event).

While Menu Designer is displayed double-click on the Open menu item, this will open up an even procedure for the OnClick event for this menu item.

procedure TForm1.mnuOpenClick(Sender: TObject);
begin

end;

Now, everything you have to do is to cut-paste the code already written inside the button's btnOpen OnClick event handler.

procedure TForm1.mnuOpenClick(Sender: TObject);
begin
 if OpenDialog1.Execute then begin
  Form1.Caption := OpenDialog1.FileName;
  Memo1.Lines.LoadFromFile
    (OpenDialog1.FileName);
  Memo1.SelStart := 0;
 end;
end;

Note: this will leave us with the "empty" OnClick event for Open button. The handler is removed when you compile or save the project.

After you create the event handlers for the menu items: Save, Find and Replace, cut-paste the code inside OnClick event handlers of btnSave, btnFind and btnReplace.

Finally, remove all four button components from the form.

Menu enabled MyNotepad at Design Time

It's time to see or project in action. Click the Run button and the program should compile and run. If there are no errors, you are presented with the full-featured not-so-complex-and-therefore-easy-to-use menu enabled MyNotepad application.

MyNotepad at Run Time

©2014 About.com. All rights reserved.