HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi tutorial: Menu, About page, Quit method.

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

IGNORE ANY PERIODS (.) AT THE START OF ANY LINE

In this tutorial, we will create a menu, an 'About' panel, and a 'Quit' button.



This tutorial is a level 2 tutorial... you only need to have grasped the material in the level 1 tutorials to undertake this.

Start a project, any project.

Add a main menu object to the form. (It is available from the 'standard' components page of the components palette.) Where you place the icon will have no bearing on the appearance of the menu when the program is run... just put it in some place out of the way on the form.

Double click on the main menu object on your form. A new window should pop up. It is in this that you will define the menu bar of your program.

A rectangle made of dotted lines will be at the left hand end of the menu bar, and the Object Inspector will be showing an unnamed object. Name it mmFile, then Caption it '&File'. (And press enter, or use an arrow key to leave the caption box)(The ampersand (&) will cause the F to be underlined, and make Alt-F an alternative to clicking on this menu item.

Click on 'File' on the menu design form.
Press the 'down' cursor key.
Make the caption for the first item in File's pulldown '&New Game', and name it mmFileNewGame. Press Enter.

Click on the dotted-line-rectangle below 'New Game'
Make the caption '&Quit', Name mmFileQuit

Click on 'File' again.
Press the 'go right' cursor key.
Make the caption '&About', Name mmAbout

You now have a basic menu, illustrating the possibilities. Close the menu design window. To extend it later, just select it (MainMenu1) in the object inspector and double click on the 'Items' field.

Now you need to add code to your program to act on the menu options.

I'm not going to do anything here about the 'New Game' item.. that is too specific to whatever application you are working on.

On your form (not on the menu design window), click on File, then click on Quit. Delphi will add the skeleton of an event handling procedure to your source code, probably calling it .mmQuitClick, and will move you to the code editing window, with the cursor ready for the code to be added. You want at least...

application.terminate;

You may want the program to do other things first, for example giving the user a chance to change his/ her mind. If the program involves an .ini file or data which may need saving, then if it has be altered since the last save, it is usual in Windows programs to check with the user:'Work not saved. Save it now/ No/ Cancel?' (Working with data from files is beyond the scope of this tutorial, but is covered in later tutorials.)

You can test your menu and the File|Quit option now.

________________

For the 'About' option, we will create some text which will appear and which can be made to go away again.

The approach taken here to doing an 'About' panel differs from that in the excellent Delphi (ver 1) manual. Each has its merits.

Before you begin, resize the whole form to something reasonable.

a) Add a panel ('standard' component) to your form. Name it pAbout. Make it as wide as your form, but only high enough to show one line of text. Move it (up or down) to an out of the way place on your form.

Panels are great because they are 'container' components. We will put things on the panel, and because they are 'contained' by the panel. we can do things to the whole collection with single actions.

b) Don't be alarmed by the result of the next change. Set the panel's Align property (not AlignMENT) to alClient.

c) Put a button on the panel. Name it bpAboutOK; Caption: OK.

d) Put a memo component on the panel. Name it mpAboutMemo; align alClient; make enabled false (Stops users changing 'About' text. Changes wouldn't be permanant, anyway.)

e) When you added the memo, your 'OK' button probably disappeared. Use the listbox at the top of the object inspector to select bpAboutOK. The little black squares mariking a selected item appear on the form. RIGHT-click somewhere within the selection squares. Select 'Bring to Front' from the menu. Move it to some tidy place. If your form gets resized, the button will only maintain its distance down and over from the upper left hand corner. If the form gets too small, scroll bars will appear.

f) Reselect mpAboutMemo. Be careful in this work that you are getting the object you want. It is easy to get mpAboutMemo instead of pAbout.

We are going to put some text on the panel. Now... I must confess: I do not fully grasp all of the details of what I'm about to delve into... but what I'm suggesting works! (The problems that you can encounter involve having 'returns' embedded in your text at places you don't want them. There's a complex interplay between the following memo properties: Enabled, Scrollbars, Wordwrap. The WantReturns property is not involved in this issue. While the memo's Enabled property is false, the setting of WantReturns is irrelevant. Setting WordWrap true won't fix everything for you... the system inserts extra returns into the text to reflect the width of mpAboutMemo when the program was compiled... but having it set true is better than having it set false: The layout may be poor, but at least no text is lost. Leave Scrollbars ssNone. They won't work while Enabled is false, anyway, nor will they automatically disappear if not needed.

(If you want to try to get to the bottom of all of this, you may be happier working with the panel's and memo's align properties set to alNone)

mpAboutMemo should be selected at this point. Double-click on the Lines property. The String List Editor pops up. Delete the default mpAboutMemo which is there. Enter the panel's text. Typical 'About' test includes the program's name, vendor address, and copyright messages. My imperfect sustem requires that you press enter near the end of lines, as in the bad old days of manual typewriters. Press enter twice to create blank lines between paragraphs. When the text is complete, click on the editor's 'OK' button. (You don't need to use the 'save' button.)

The system is good at adding new returns if the memo isn't wide enough... but it isn't good at taking them out again if the memo gets wider.

You may want to set the memo's Alignment property to alCenter. This centers each line of the memo's text in the space available.

Anyway... it may not look perfect, but there should be some useful content, anyway.

__________
Once you are happy with your text, you need a way to 'hide' it while you work on the rest of the project. Set pAbout's Align property to alNone and resize the panel to be very narrow vertically. Horizontally, don't make it narrower than the form. If you want extra 'hiding', select the panel, right-click, click on 'Send To Back'.

__________
Now we come to writing the code to make the About panel appear and disappear.


a) Create an OnCreate event for the form, if there isn't one already. Put the following in it:

pAbout.hide;


b) Create an OnClick event for mmAbout as follows:

pAbout.show;
pAbout.bringtofront;
pAbout.align:=alClient;
bpAboutOK.setfocus;

c) Use the object inspector to select bpAboutOK. (It doesn't matter if you have the panel minimized.) Create an OnClick event which says

pAbout.hide;


And there you have it!

__________
An extension:

If you like to put version indicators into your programs, as you should, you can fool around with the Lines property of mpAboutMemo each time the version number changes, or you can....

Select pAbout, set Align alNone.
Add an edit box. Call it epAboutVersion. Be sure it is in front of the memo. Make Autosize false, Enabled false.

In the FormCreate procedure, add epAboutVersion.text:='Version 0.00';

(Labels don't work well for something like the Version field. They are always under controls (e.g. memos or buttons) on the panel. Furthermore, unless their Transparent property is made True, they can blot each other out. 'Sent to Front' still has a bearing, but only in 'stacking' the labels, all of which are under the controls.)




   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search


Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.


Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....