Monday, January 10, 2022

Displaying hints (2)

Continuing from yesterday's presentation about hints in a non-modal form, a more modern way of writing the code that I showed yesterday for hooking the application.onhint method would be

procedure TMyForm.ApplicationEvents1Hint(Sender: TObject); begin StatusBar1.SimpleText := Application.Hint; end;

This achieves the same result as my naive code but is much simpler. Unfortunately, it is also wrong as it again causes the active form to display hints that arise from other forms. Somehow there should be a way of checking from where the hint arises. Apparently the ApplicationEvents.OnShowHint event exposes the HintInfo object that includes a pointer to the control that created the hint, but I haven't seen an example of this, and the structure that I described yesterday (a grid on a tabsheet on a page control on a form) is liable to confuse this event.

The given solution is actually quite simple and depends on a function that tests whether the control creating the hint resides upon the current form.

function IsHintFor(AForm: TCustomForm): Boolean; var local: TControl; begin Result:= False; local:= FindDragTarget (Mouse.CursorPos, True); if Assigned (local) then Result:= GetParentForm (local) = AForm; end;

One places on every form that requires the hint handling a TApplicationEvents component, whose OnHint event is as follows

procedure TMyForm.ApplicationEvents1Hint(Sender: TObject); begin StatusBar1.SimpleText := Application.Hint;
end;

Easy peasy. So yesterday evening I went through the 'ERP' application and changed the hint-handling code in about 30 forms. I also added the above to a form template, meaning that I won't have to define this manually in the future.

No comments: