Chapter 29 Self-Assessment Quiz: Menus, Dialogs, and Application Structure
Section 1: Multiple Choice
Q1. In a TMainMenu, the & character in a menu item's Caption creates:
(a) A bold character (b) An underlined accelerator key activated with Alt (c) A keyboard shortcut that works without opening the menu (d) A separator line
Answer
(b) The& prefix underlines the following character and makes it an accelerator key. For example, &File displays as File and responds to Alt+F. This is different from a ShortCut, which works without the menu being open.
Q2. What does MessageDlg return when the user clicks the "No" button?
(a) mrOK
(b) mrCancel
(c) mrNo
(d) False
Answer
(c)MessageDlg returns a TModalResult value corresponding to the button clicked: mrYes, mrNo, mrOK, mrCancel, mrAbort, mrRetry, or mrIgnore.
Q3. Which event should you handle to prevent the application from closing when there are unsaved changes?
(a) OnClose
(b) OnCloseQuery
(c) OnDestroy
(d) OnDeactivate
Answer
(b)OnCloseQuery fires before the form closes and provides a CanClose parameter. Setting CanClose := False prevents the form from closing. OnClose fires after the decision to close has been made, so it is too late to prevent closing.
Q4. What is the purpose of TActionList?
(a) To store a list of recently performed operations for undo/redo (b) To centralize actions so multiple UI elements (menus, toolbar buttons) share the same code, state, and properties (c) To record user actions for playback as macros (d) To manage keyboard shortcut conflicts
Answer
(b) TActionList centralizes command logic. Each TAction defines its OnExecute handler, OnUpdate handler, Caption, ShortCut, ImageIndex, and Enabled state. Multiple UI elements (TMenuItem, TToolButton) can reference the same action, ensuring they stay synchronized.Q5. The DefaultExt property of TSaveDialog:
(a) Sets the default file filter (b) Automatically appends the extension if the user does not type one (c) Changes the dialog title (d) Restricts which file types can be overwritten
Answer
(b) If the user types "expenses" without an extension andDefaultExt is set to 'pw', the dialog automatically saves as "expenses.pw". This prevents users from accidentally saving files without extensions.
Q6. To make a toolbar separator between button groups, you add a TToolButton with:
(a) Caption := '-'
(b) Style := tbsSeparator
(c) Enabled := False
(d) Visible := False
Answer
(b) Setting a TToolButton'sStyle to tbsSeparator renders it as a visual divider between groups of related buttons, similar to how a '-' caption creates a separator in a menu.
Section 2: True or False
Q7. A TPopupMenu's OnPopup event is the right place to enable or disable context menu items based on the current selection.
Answer
True. TheOnPopup event fires just before the context menu is displayed, making it the ideal place to check the application state and enable or disable items accordingly. For example, disable "Edit" and "Delete" when no item is selected.
Q8. TIniFile can only store string values.
Answer
False. TIniFile has methods for reading and writing multiple types:ReadString/WriteString, ReadInteger/WriteInteger, ReadBool/WriteBool, ReadFloat/WriteFloat, and ReadDate/WriteDate.
Q9. A TAction's OnUpdate event is called automatically by the framework — you do not need to call it manually.
Answer
True. The LCL callsOnUpdate for each action during idle processing (when the application is waiting for events). This ensures that UI elements reflect the current application state without you writing polling or synchronization code.
Section 3: Short Answer
Q10. Write the code to create a TOpenDialog that shows only PNG and JPEG image files, with a default filter of PNG. If the user selects a file, display its full path in a ShowMessage.
Answer
procedure TForm1.btnOpenImageClick(Sender: TObject);
begin
OpenDialog1.Title := 'Select Image';
OpenDialog1.Filter := 'PNG Images (*.png)|*.png|JPEG Images (*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files (*.*)|*.*';
OpenDialog1.FilterIndex := 1;
if OpenDialog1.Execute then
ShowMessage('Selected: ' + OpenDialog1.FileName);
end;
Q11. Explain why the title bar convention * Untitled — PennyWise is important. What does the asterisk communicate to the user?
Answer
The asterisk is a universal convention indicating unsaved changes. It tells the user at a glance that their data has been modified since the last save. Without it, users might close the application thinking their changes were saved. The format[modified indicator] [filename] — [application name] is standard across platforms (used by Notepad, VS Code, LibreOffice, etc.).
Q12. You have an action actBold that toggles bold text in a TMemo. Write the OnExecute handler that toggles the font style, and the OnUpdate handler that checks/unchecks the action based on the current state.
Answer
procedure TfrmMain.actBoldExecute(Sender: TObject);
begin
if fsBold in mmoContent.Font.Style then
mmoContent.Font.Style := mmoContent.Font.Style - [fsBold]
else
mmoContent.Font.Style := mmoContent.Font.Style + [fsBold];
end;
procedure TfrmMain.actBoldUpdate(Sender: TObject);
begin
actBold.Checked := fsBold in mmoContent.Font.Style;
end;
The menu item and toolbar button automatically show/hide the check mark based on the action's Checked state.