Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show hints for items in a ListBox? 1

Status
Not open for further replies.

corsair2

IS-IT--Management
Feb 21, 2001
55
GB
Hi Folks

I'm using a listbox on a form which is likely to have items too long to display in the listbox. Unfortunately, I'm not able to make the listbox larger.
Does anyone know how I can provide my users with the type of hints found in, for example, Windows Explorer, where the hint appears when the mouse is held over an item displaying the full name when it is not fully visible in the window?

 
The following process will show hints for list box items that are too wide to be shown. It won't show hints for those items that are within the width of the listbox.

1) Set the ListBox's ShowHint property to true.

2) Declare the following routine as a private procedure of the form (you'll need to include ComCtrls in your uses list in order to recognise a TListItem):
Code:
procedure TForm1.AppShowHint(var HintStr: string; var CanShow: Boolean;
          var HintInfo: THintInfo); 
{displays the full length of hidden text in TListBox as a hint}
var 
  LBIndex, i, ColLeft, ColRight, Col : integer;
  LI : TListItem;
  ColText : string;
  TN : TTreeNode;
  TextRect : TRect;
begin
  CanShow := false;
  with HintInfo do begin
    if (HintControl is TListBox) then
      with TListBox(HintControl) do begin
        LBIndex := ItemAtPos(CursorPos, true);
        if LBIndex > -1 then begin   // listbox item under mouse
          {test width and show hint if necessary}
          if Canvas.TextWidth(Items[LBIndex]) > Width then begin
            HintPos := ClientToScreen(Point(-1, (ItemHeight * (LBIndex - TopIndex)) - 3));
            HintStr := Items[LBIndex];
            ReshowTimeOut := 500;
            HideTimeOut := 2500;
            CanShow := true;
          end; {Canvas.TextWidth(Items[LBI]) > Width}
        end; {if LBI > -1}
      end; 
  end;
end;

3) Then put this code into the form's create method:
Code:
  Application.OnShowHint := AppShowHint;

Reference:

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top