Delphi Programming for Beginners - Lesson 8Back to the description of often used components
This component symbolises main menu of the application with all submenus. After placing the component on a form, we can open the designer by clicking on the icon. Generally speaking, it is convenient to combine main menu with component ActionList which does a lot of work for us, but right now we won’t need it.
TMainMenu is basically an encapsulation for components TMenuItem which symbolise individual items of menu. I think I have already written that it’s convenient for each type of components to use different prefixes (for example Tlabel components start with lbl: lblName or lblAge and for these labels we have TEditBoxes called edtName or edtAge - it makes you code much more clear).
It is a good practise to first fill attribute Name and then do the same for attribute Caption, otherwise IDE will generate the name based on attribute Caption. This way the whole menu including submenu could be generated.
IMPORTANT: If we set caption as “-“, the menu item is changed to separator.
Clicking on an item in designer generates appropriate event OnClick where we can specify what happens when a user click on this particular item. Here’s an example of a code for item Exit:
procedure TForm1.miExitClick(Sender: TObject); begin Application.Terminate; end;
If we want an icon for each item, we put component TImageList on a form and we assign it to our component main menu. We then set ImageIndex for each item (just like for other components). Interesting option is to set shortcut key using attribute Shortcut for menu items. Attributes Enabled or Visible work just like with other components, on top of that there’s also property Checked which will show “check” next to the item. Last but not least there’s an option of own drawing items which is a bit more complicated but it allows programmers to do amazing stuff. Maybe we will come back to it one day.TPopUpMenu
This component is very similar to the previous one with only difference being that it can be on the form several times and it’s not visible initially because it symbolises popup or context menu (that is a menu shown after right-click.
Items are defined the same way we define them in TMainMenu. For a component we want to add a popup menu to we just set property PopupMenu. Programme then makes sure that for each component the selected menu will be shown.
You can also call pop menu manually, for example after pressing mouse button. Let’s say that we have pop menu pm1 and a button on the form. If we put this chunk of code into button’s event OnClick then the pop menu will appear where our mouse cursor is.
procedure TForm1.btn1Click(Sender: TObject); begin pm1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y); end;
When I was describing TPanel (and others), I forgot about this very important component. Its purpose is to divide components of TPanel type but so that user can change the ratio of division just by dragging the mouse. It is a bit difficult to describe this actually very used and well-known component which you can see in the picture below where it separates the tree and the items.
In the following example you may see these components:
- panel which isn’t visible but which contains panel pnl3, TMemo mem1 and TSplitter between them
- I first inserted panel pnl3 set its Align to alLeft
- Then I inserted TSplitter which has Align set on alLeft automatically
- Eventually I inserted TMemo and set Align to alClient
- The last thing I did was setting splitter’s width to 10 so it’s easier for a user
Our last component for today is a timer.
It is a very simple component with only two properties and one event, but it’s very useful.
Properties are Interval which is how often a certain event will be called, Enabled determines whether a timer is active and event OnTimer is a called event.
I put timer tmr1, Label lblTimer and button btnStart to our previous example. I set interval for timer to 100 ms and Enabled to false (timer isn’t active). I added following code:
procedure TForm1.tmr1Timer(Sender: TObject); begin lblTimer.Caption := DateTimeToStr(Now); end;
So that every 100ms current time is put as a text for label. The last thing to do is to enable timer in button’s body.
- add button stop for the last example (with the caption of the label being STOP)
- Add an item to the main menu which will enable and disable menu’s item Exit
- Add one more panel under pnl3 and mem1 which will be separated from both by one splitter (the original ones are 1 and 2, new panel is 3)
Sample project regarding homework can be downloaded from here.
About the AuthorIng. Radek Červinka
More interesting links and pages
Visit GambleGenie.fi to get the best casino value in Finland
If you are going to play online casino in Canada you better visit the Genie first
Discover UK licensed casino sites at Casinopilot
Try out new casino sites at SmartphoneCasinos.co.uk
Indian casinos are here to stay and Royal Panda is your top choice
Play blackjack at the best live dealer casino online in the UK
Our favorite online real-money casinos in Austria on Casinohex.at is a great opportunity to enjoy all the benefits of playing for real money.