Chapter 28 Self-Assessment Quiz: Forms, Controls, and Events


Section 1: Multiple Choice

Q1. Which control should you use for a selection where only one option can be chosen from a group?

(a) TCheckBox (b) TRadioButton (inside a TGroupBox or TRadioGroup) (c) TComboBox with Style = csDropDown (d) TListBox with MultiSelect = True

Answer (b) Radio buttons enforce mutual exclusion — selecting one deselects the others in the same container. Checkboxes allow multiple independent selections. A TComboBox also works for single selection but presents a dropdown rather than visible options.

Q2. What does Anchors := [akTop, akLeft, akRight] do to a control when its parent form is resized wider?

(a) The control moves to the right (b) The control stays the same size and position (c) The control stretches horizontally to maintain its distance from both the left and right edges (d) The control centers itself horizontally

Answer (c) The control is anchored to the left, top, and right edges. As the form widens, the right edge moves, and the control stretches to maintain its distance from both the left and right edges. The top edge stays fixed.

Q3. What is the purpose of ModalResult on a button in a modal form?

(a) It sets the button's tab order (b) It determines what value ShowModal returns when this button is clicked (c) It controls whether the button is visible (d) It specifies the button's accelerator key

Answer (b) When a button with a non-zero ModalResult is clicked, the modal form closes and ShowModal returns that ModalResult value. Setting ModalResult := mrOK on the OK button and ModalResult := mrCancel on the Cancel button is the standard pattern.

Q4. Which validation strategy fires on every keystroke?

(a) On-submit (b) On-exit (c) As-you-type (OnChange) (d) On-create

Answer (c) The OnChange event fires every time the control's content changes — which is every keystroke in a TEdit. On-exit fires when the control loses focus. On-submit fires when the user clicks a save/submit button.

Q5. To create a control at runtime and make it visible on a panel, which two properties must you set?

(a) Owner and Name (b) Parent and Visible (c) Parent and OnClick (d) Parent is the key one; Owner is set in the constructor

Answer (d) You must set Parent to the container where the control should appear. The Owner (set in the Create constructor) determines who frees the control. A control without a Parent exists in memory but is not displayed. Visible is True by default.

Q6. What is the difference between TPanel and TGroupBox?

(a) TPanel can contain controls; TGroupBox cannot (b) TGroupBox has a visible caption and border; TPanel is typically invisible/flat (c) TPanel is for data display; TGroupBox is for data input (d) There is no significant difference

Answer (b) Both can contain child controls. The key difference is visual: TGroupBox has a labeled border (its Caption appears as a title), making it suitable for semantically grouping related controls like radio buttons. TPanel is typically flat or has a subtle bevel, making it suitable for structural layout (toolbars, regions, containers).

Section 2: True or False

Q7. A TStringGrid can only display text; it cannot respond to user editing.

Answer False. Adding goEditing to the grid's Options property allows the user to edit cells directly. You can also use the OnSetEditText event to respond to edits.

Q8. When using ShowModal, the calling code pauses until the modal form is closed.

Answer True. ShowModal is a blocking call. The line after ShowModal does not execute until the modal form closes. This is unlike Show (modeless), which returns immediately.

Q9. Setting cboCountry.Style := csDropDownList prevents the user from typing free text in the combo box.

Answer True. csDropDownList restricts the user to selecting from the predefined items. csDropDown allows both selection and free-text entry.

Section 3: Short Answer

Q10. Write a shared event handler for three buttons (btnSmall, btnMedium, btnLarge) that changes the form's font size to 8, 12, or 16 respectively. Use the Tag property.

Answer
{ Set Tag values in FormCreate or Object Inspector: }
{ btnSmall.Tag := 8; btnMedium.Tag := 12; btnLarge.Tag := 16; }

procedure TForm1.FontSizeClick(Sender: TObject);
begin
  Font.Size := (Sender as TButton).Tag;
end;
Assign all three buttons' OnClick to FontSizeClick in the Object Inspector.

Q11. You have a form with edtName (TEdit). Write an OnExit handler that turns the edit field's background yellow if the text is empty, and restores it to the default color otherwise.

Answer
procedure TForm1.edtNameExit(Sender: TObject);
begin
  if Trim(edtName.Text) = '' then
    edtName.Color := clYellow
  else
    edtName.Color := clDefault;
end;

Q12. Explain why setting Align := alClient on two different controls in the same parent does not work as expected. What happens?

Answer Only one control should have Align := alClient per parent. If two controls both have alClient, the second one (in z-order) will attempt to fill the same space as the first, and one will end up covering the other or having zero size. The alClient property means "fill all remaining space after other aligned controls," so the first alClient control takes all remaining space, leaving nothing for the second.