Home
What's New
Delphi 3.0 Tips
Questions
Development Tools
Links

Make a ListBox's Hint the Item the Mouse is Over

What you basically do is use the Mouse Move event of a ListBox to call a procedure that sets the Hint of the ListBox depending on the X, Y coordinates of the mouse. The function in this case is ListBoxPosToHint. The ListBoxPosToHint procedure should be put in a file that is visible to all Forms with a ListBox that will use the procedure.
//uses Tag as Last ItemAtPos(X,Y); should be set to -1 OnEnter
procedure ListBoxPosToHint(var ListBox : TListBox;
const Application : TApplication; X, Y : integer);
var i : integer;
begin
  with ListBox do begin
    i := ItemAtPos(Point(X, Y), true);
    if i <> Tag then begin

      //the hint won't change with out calling CancelHint
      Application.CancelHint;
      if i = -1 then
        Hint := ''
      else
        Hint := Items[i];
    end;
    Tag := i;
  end;
end;

procedure TForm.ListBoxEnter(Sender: TObject);
begin
  ListBox.Tag := -1;//used as last item index for ListBox
end;

procedure TForm.ListBoxMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ListBoxPosToHint(ListBox, Application, X, Y);
end;