TMainMenu

From Free Pascal wiki
Revision as of 14:42, 8 April 2013 by Rfc1394 (talk | contribs)
Jump to navigationJump to search

>> LCL Components >> TMainMenu
This page explains how to use the TMainMenu component. When I mention to click on something, unless I explicitly say to right-click, you always left-click on the item in question.

It is common practice to name menus starting with mnu or menu and the name of the menu. Submenus continue this by prefixing with the menu they are within, e.g. the 'Cut' submenu in the Edit top-level menu is usually named mnuEditCut or menuEditCut. This is a mnemonic and makes it easier to remember, six months from now when you need to make changes, how to put them in. Let's get started. Note that this is a convention, it isn't mandatory, but if you do it this way it will probably make it easier to make changes later, or to understand what the code is doing when you haven't been looking at it for a while, or to allow someone else who has to do maintenance on the program you're writing to be able to fix it later.

  1. Select TMainMenu from the component bar and place a component on your form by clicking on the TMainMenu component, then click on the form but do not let go of the mouse button, and while holding the mouse button down, draw a box and let go of the button. The component will appear on your form. This will be a square with a representation of a drop-down menu and the component's name, which will default to MainMenu1.
  2. If you don't like the name MainMenu1, go to the Object Inspector window and change the Name property to something you like better. Let's say we change it to XMenu. Type in XMenu in the box to the left of the Name property, and push enter. The name on the component changes.
  3. Right-click on XMenu, and a pop-up menu will appear. For right now, what we want is the first selection, Menu Editor. Click on it.
  4. The Menu Editor window will open with a menu item already created with a caption of "New Item1". This will be the top-level menu, similar to the "File Edit View Help" menus you've seen before. You probably want to change this, so click on it, then go to the Object Inspector.
  5. In object inspector, change the Name property from MenuItem1 to something more appropriate. Let's say this is the File menu, so let's change Name by typing in mnuFile and press enter.
  6. We want a better caption than New Item1, so go to the Caption property and type in &File and press enter. The Ampersand & in front of the name is an accellerator, that's what allows you to open a menu by pressing the Alt key and the underlined letter. The caption for the menu changes to File.

It is at this point that you create additional top-level menus.

  1. Go back to the Menu Editor window. Click on File, then right-click on it. A pop-up menu will appear. Click on Insert New Item After, and a new menu, called New Item2 will appear. As explained in the last two items, let's change its name to mnuHelp and the caption to &Help in the Object Inspector.
  2. Let's make a menu item under File. Right-click on it, then click on Create Submenu. The file menu now has an arrow on it, and a submenu called New Item2 appears.
  3. Change this to something related to what it is to do, let's say "Open". Go to the Object inspector, change the Name property to mnuFileOpen, then change the caption to &Open.
  4. We need another top-level menu item between File and Help. You can either right-click on File and click on Insert New Item (after) or right-click on Help and click on Insert New Item (before)
  5. Change this item's name property in the Object Inspector to mnuEdit and its caption property to &Edit.
  6. Continue the above accordingly for each menu and submenu you need.

Now, all this will get you is a menu that displays at run time and will allow the user to click on the menus. It won't actually do anything. To have the menu items do something, you have to add events for each menu or submenu that is to react to being clicked upon. Usually, top-level menus don't react, the submenus do. You have two choices on how to have the menu react; you can insert the events into the menu, or you can use a TActionList component. The main reason for using a TActionList is if you plan to have an icon toolbar, say that you have a set of menus with "File" then New, Open, Save, Save As, Quit, etc. as submenus, and you're going to have a toolbar with New, Open, Save and Save As as buttons as well. Rather than writing two routines to handle the New and Open functions, you use a TActionList for both the Menu and the toolbar. How to do that using a TActionList will be explained there. For the mean time, I'll explain how to handle a menu click using an event in the Object Explorer.

  1. Go back to the Menu Editor, click on the Open submenu under File. Go to the Object Inspector window, click on the properties tab. The only event you really want to change is OnClick, which is blank. If you had an existing event handler, you could use it, but since you don't, you can get Lazarus to create it for you. On the right is a button with 3 dots. Click on it, and a new procedure is created in your code. It will look similar to this.
    procedure TfrmMain.mnuFileOpenClick(Sender: TObject);
    begin

    end;
  2. between tbe begin and end statements you would write the code for handling the Open action on the menu. This might include placing a TOpenDialog control from the Dialogs Tab on your form, and manipulating that dialog to create the standard 'Open' dialog. Same thing applies if you have a Save or Save as submenu.
  3. You repeat the above at the point where I mentioned how to start creating additional menus and submenus, and for each one that the user can click upon, you would create handlers for each menu option as needed.

There are other things you can do such as enabling your program to support Internationalization (menus in other languages), icons, check-mark menu options, and so on. These may be explained later.


Return To: LCL Components Next: TPopupMenu


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu