Multi Line Popup Hints for Delphi VCL and FMX

Learn how to setup multi line popup hints in Delphi for any type of control for both VCL and FireMonkey.
This uses native Delphi code.  You don’t need to hack the forms’s DFM text file

multi_line_hints_3

Take a short cut … download and use

Cant be bothered reading all this stuff ?
Just do this

  1. Download my utility units for VCL and FireMonkey.
    See the download link at the bottom of the page.
  2. Add a “~” character in your hint text.
    It will be replaced at runtime with a new line
  3. Then include this magic code in your form’s OnCreate event.
begin
  SetupMultiLineHintAll (self);
end;

One line of code .. .easy !

Read on if you want to geek out on the full details

The gory detail

This technique uses a flag character in the hint text (I like to use the tilde character ‘~’) that is replaced at runtime with an ASCII new line character (ASCII #10)

This is what it looks like in the IDE

multi_line_hints_1

This magic code will change the ~ character to a new line character

begin
  Button1.Hint := AnsiReplaceStr (
                         Button1.Hint,
                         '~',   // replace this
                         #10    // with this
                         );
end;

So now when you hover over the button the popup hint text will show in multiple lines like this

multi_line_hints_3

Generic Procedure

Instead of hard coding for one button, you can use a generic procedure that will work for any type of control.

These procedure will work in both VCL and FMX.
You just have to USE different units, as mentioned in the comments

procedure SetupMultiLineHint (aComponent : TComponent);
 // setup multi line hints for a single control
 // For VCL: uses VCL.controls, System.StrUtils
 // For FMX: uses FMX.controls, System.StrUtils
var
 s : string;
begin
 if aComponent is TControl then
    begin
    s := AnsiReplaceStr (TControl(aComponent).Hint ,'~',#10);
    if TControl(aComponent).Hint <> s then
       TControl(aComponent).Hint := s;
    end;
end;

UPDATE – Thanks To Mike for the bug report about TMenuItems not working (see comments below).  Mike’s recommended changes have been implemented above

Then call it like this

begin
  SetupMultiLineHint (Button1);
  SetupMultiLineHint (Button2);
  SetupMultiLineHint (Edit1);
  SetupMultiLineHint (Label1);
end;

Apply To All Controls In A Form

The above example required you to specify each control.

You can can go one step further with a generic procedure that will setup multi-line hints for all controls on your form

procedure SetupMultiLineHintAll (
           aComponent : TComponent // form,frame,button,label,panel
           );
 // setup multi line hints for a component and all child components
 // convert all ~ characters in hints a new line/
 // For VCL uses VCL.controls,System.Classes
 //             ,VCL.Forms,System.StrUtils;
 // For FMX uses FMX.controls,System.Classes
 //             ,FMX.Forms,System.StrUtils;
var
 i : Integer;
begin
 if (aComponent is TControl) then // form and frame hint
    SetupMultiLineHint (aComponent as TControl);

     // Loop through all the components on the form
     // This finds components anywhere on the form
     // even within panels, tabs and frames
 for i := 0 to aComponent.ComponentCount -1 do
     if aComponent.Components [i] is TFrame then
        SetupMultiLineHintAll (aComponent.Components [i] as TFrame)
     else
        SetupMultiLineHint (aComponent.Components [i] as TControl);
end;

Then call it like this from the form’s OnCreate event

begin
  SetupMultiLineHintAll (self);
end;

Trouble Shooting

  • Popup hints do not display for TLabels in FireMonkey Delphi 10.1 Berlin)
    I think this is a bug in 10.1 FMX.  It has nothing to do with multi-line hints as it occurs with just one line of text
  • #10 doesnt work
    #10 should work on all platforms, but I havent been able to test that due to my Mac Book being temporarily out of action.
    If it doesnt work, try the old trick of using all combinations of #10 and #13
    #10
    #13
    #10 + #13
    #13 + #10

Alternatives

You can also implement multi-line hints by modifying the text version of the form.  Zarco Gajic explains how to do that here

Download

Download
This includes one unit for VCL and another for FMX.
Tested on Delphi 10.1 Berlin

To implement it, use this code in your forms OnCreate event

begin
  SetupMultiLineHintAll (self);
end;

+1 this post

I am trying to get this blog listed on DelphiFeeds.com
If you like this post, please +1 for me here on Delphi Feeds

and here on BeginEnd

Thank You !

About The Author

scott_circle
The Usual Suspect
– Scott Hollows –

  • Oracle and Delphi software developer.
  • Australian Delphi User Group – Western Australia Chief Cat Herder
  • Australian Delphi User Group – President
blog email linkedinlogo

Published by

4 responses to “Multi Line Popup Hints for Delphi VCL and FMX”

  1. Might want to add a check for the else otherwise items like TMenuItems will throw a runtime error.

    for i := 0 to aComponent.ComponentCount -1 do
    if aComponent.Components [i] is TFrame then
    SetupMultiLineHintAll (aComponent.Components [i] as TFrame)
    else if aComponent.Components[i] is TControl then
    SetupMultiLineHint (aComponent.Components [i] as TControl);

  2. Thanks Mike. Good point

    I did further tests and found it also had problems with TAction, TActionList ,TMainMenu ,TMenuItem, TPopupMenu. The hint property lives in TControl and all of those descend from TComponent not TControl so I need to handle that.

    Ive updated the SetupMultiLineHint procedure to deal with this.

    It now accepts a component (instead of a control) and it checks to see if it is a control before modifying the hint.

    -== Scott

  3. Thank you so much Scott. You really helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: