22 Votes

Delphi/Lazarus: Display Text of long ListBox Items as Hint

Tutorial by Stefan Trost | Last update on 2023-01-07 | Created on 2014-01-06

In this tutorial I will show you how to implement using Delphi or Lazarus that the the text of long TListBox Items is displayed as hint (a small box at a mouseover event) when you hover the mouse over the item.

Unfortunately, this is not a feature of the ListBox itself (in contrast to the TListView) and because the ListBox also does not support any line breaks, it makes sense to implement this feature programmatically.

Always show Hints

First let's look at this code, we have added to the OnMouseMove event of our listbox. In this example, we always show the hint, no matter how long the text of an item may be.

procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  k: integer;
begin
  k := ListBox1.ItemAtPos(Point(X,Y),true)

  if k = -1 then begin
    ListBox1.ShowHint := false;
  end else begin
    ListBox1.ShowHint := true;
    ListBox1.Hint := ListBox1.Items[k];
  end;
end; 

When moving the mouse over our ListBox, the OnMouseMove event is automatically executed and passing the mouse coordinates in its X and Y variables. Using the function ItemAtPos(), we can determine the entry of a ListBox located at a certain location. The function returns the index of the relevant item, or -1 if there is no entry at this point.

So, depending on the return value, we have to enable or disable the ShowHint property of the ListBox and set the hint to the text of the corresponding item.

View Text only for long Items

If we only want to show the text for items of a certain length (for example, for those that extendthe size of the ListBox, we can add a further condition to our code:

procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  k: integer;
  s: string;
begin
  k := ListBox1.ItemAtPos(Point(X,Y),true)

  if k = -1 then begin
    ListBox1.ShowHint:=false;
  end else begin
    s := ListBox1.Items[k];
    if ListBox1.Canvas.TextWidth(s)>ListBox1.Width-10 then begin
      ListBox1.ShowHint := true;
      ListBox1.Hint := ListBox1.Items[k];
    end else begin
      ListBox1.ShowHint := false;
    end;
  end;
end; 

So, if finding an item under our cursor, first, we check the length of the item text for extending the width of the ListBox with the help of Canvas.TextWidth(). Only in this case, we activate our Hint, otherwise we set ShowHint back to false with which the Hint will not be shown.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.