5.06.2013 How to show ToolTip, Balloon Tip and other short help text

Tooltip icon decoration
Tooltips are text that appears as a short help for a component when we keep our mouse over it for a while. We can add single line or multiline tooltips easily. Here's how...


Why Tooltips / Hints?

Tooltips are a popular and common feature in the modern Operating Systems. Tooltips are also seen in HTML webpages (by using title attribute to a tag). It is a great way to let the user understand what a button or a particular clickable object does. It is excellent for the users who are learning the operation of your software by himself, giving him a hint of the function of an object/component.

Example of Tooltip in Microsoft Paint


Tooltips also help the interface to be cleaner. If you have to put label Texts all around the form to just explain the functions of the buttons (or components), then your design would be cluttered. Tooltip saves that valuable space in your User Interface design and makes your forms look neat.

We are also going to look at some other Help options which keep the form neat as well: Popup Notifier, Balloon Tooltip and status bar text help.

How to use Tooltips for a component?

First, select the component for which you want Tool Tip for. Go to the Object Inspector window. Scroll a little and find an item titled "Hint". You can write the Tooltip Text that you want in the box next to it, or you can click the button at the right to enter a Tooltip/Hint of multiple lines.

Showing how to add single line and multiple line Tool tip in Lazarus GUI


Second, find out another property titled "ShowHint" and set it to True. If you don't set it to True then the Tooltip will not appear.

You can now Run your program (F9 or Run->Run). Hover your mouse over the component and you will see a Tooltip (or Hint) explaining what the button does!

Variation 1: PopupNotifier

Another variation is the TPopupNotifier which can be used as a popup help which is better for long help.

You can use it like this:
Drop TPopupNotifier (from Common Controls tab). From its properties set an Icon, Text and Title. Create a help button (or BitBtn) somewhere and on its click event enter (we are assuming the button name is Button2):

Layout to test TPopupNotifier

  PopupNotifier1.ShowAtPos(Left+Button2.Left, Top+Button2.Top+80);

Now Run the program and click on the button. You will see a popup help.

Popup help window made in Lazarus

Variation 2: TrayIcon's Balloon Tooltip

Another modern variation is the Balloon Tooltip (or ScreenTip). For that we can drop a TTrayIcon (from Additional tab) and we need a TButton to run the ShowBalloonHint command for our Balloon Tooltip to appear. Set the BalloonTitle and BalloonHint of TrayIcon1 as you wish and set Visible to True. You should also select an Icon for it to appear in the System Tray.

Form design for Balloon Tooltip

Double click the button and enter:

  TrayIcon1.ShowBalloonHint;

Now Run the program and click the button. You will notice a Balloon Tooltip in the System Tray Area.

Balloon tooltip example made with Lazarus

Variation 3: Help in Statusbar

Another variation is the one which is also used in websites, showing help text or hint in statusbar when the user points his mouse cursor over a component.

This can be done with a universal procedure like below to update the status bar text:

var
  MyPoint : TPoint;
begin
  MyPoint := Button4.ScreenToClient(Mouse.CursorPos);
  if (PtInRect(Button4.ClientRect, MyPoint) then
  begin
    // Mouse is inside the control, do something here
  end;
end;
[ Source: StackOverflow.com ]

Or it is also possible to change the statusbar text on hover event. It is an easier version to implement but it requires to write mouseover event procedures for every button we desire a statusbar help for. We could use the first one for better option and flexibility.

You can create a TStatusBar and set its SimpleText to something you desire.

Let's do this practically. drop a TStatusbar (from Common Controls tab). Add a button. We assume its name is Button4. Now set its Hint property to something you desire. But don't set ShowHint to True (we do this because we don't want tooltips but we want to show the text in Hint property in StatusBar).

We will have to use PtInRect function. For this add "types" in the uses clause:
uses

..., types;

Drop a TTimer in the form (from System tab). Set its interval to 500. Double click it and enter:

var
  MyPoint : TPoint;
begin
  MyPoint := Button4.ScreenToClient(Mouse.CursorPos);
  if (PtInRect(Button4.ClientRect, MyPoint)) then
  begin
    // Mouse is inside the control, do something here
    StatusBar1.SimpleText:=button4.Hint;
  end
  else
  begin

    // the default text
    StatusBar1.SimpleText:='Hello!';
  end;
end;

Now run the Program and hover your mouse over the button. You will see the text in the Hint property of the Button4.

Mouse hover help text on statusbar made in Lazarus

Download Sample Code ZIP

Download example source code zip file from here: http://db.tt/D3EwslOn
Or here: http://bit.ly/13l2ycV
Size: 540 KB

The zip file contains compiled executable EXE file.
Image: http://jquerytools.org

2 comments:

mogyee said...

Great stuff as always!

is there a way to add more lines to baloonhint. Mine is maxing out at 8 lines. Cheers.

Adnan Shameem said...

@mogyee
Hello

Sorry, I am not sure how to show more than 8 lines that you get. (I get 5 lines max. on my Lazarus 1.8.) The height is calculated internally in the TTrayIcon component. As much as I looked, I did not find any property to modify it. But you are always welcome to search more, if it is important to you.

I think Baloonhint is meant for showing short hints only. So it is designed to show less. If more text would be allowed, it would take more screen space and would loose the point of a short and helpful hint.

You can shorten your text to only show the gist of what you want to say. This way it will be helpful, yet short.
Regards

 
Copyright 2013 LazPlanet
Carbon 12 Blogger template by Blogger Bits. Supported by Bloggermint