Hints for long Listbox items lazarus / 31-LCL ---------------------------------------------------------------------- William Marshall · 2014-11-12 18:14 For Listbox items whose length is greater than the width of the Listbox, I'd like to show the full text of the item in a hint when the mouse hovers over it.  I'm using the following code (lbDisplayList is TListbox):  Code: [Select]procedure TfrmMainForm.lbDisplayListMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Index, ItemWidth, BoxWidth : integer;     Item : string; begin   Index := lbDisplayList.ItemAtPos(Point(X,Y), false);   if Index >= 0 then     begin       Item := lbDisplayList.Items[Index];       ItemWidth := Canvas.TextWidth(Item) + 10;    //  - so lines that are "a little bit" too long are considered too long       BoxWidth := lbDisplayList.Width;       if ItemWidth > BoxWidth then         begin           lbDisplayList.Hint := Item;           lbDisplayList.ShowHint := true;         end       else begin       //  lbDisplayList.Hint := '';          // This line is not necessary         lbDisplayList.ShowHint := false;       end;     end; end;      This almost works.  The hint appears when I hover over a long string and disappears when the mouse moves to a short string that is directly above or below the long one.  But if there are two adjacent long strings, the hint for the first one remains when the mouse goes over the second one.  How can I make the hint change to the text of the second string?  Also, how do I shorten the delay between the mouse being over an item and the appearance of the hint?      (I know - using a scrollbar is much simpler.  But the Hint method is bothering me now, so I have to figure it out.)      Thanks for your help. ---------------------------------------------------------------------- wp · 2014-11-12 19:38 I'd have a look at TCustomGrid (or its descendants) - there is a solution for a similar case. ---------------------------------------------------------------------- Mike.Cornflake · 2014-11-13 16:08 If you do find a working solution, could you post it here?  I have exactly the same need...  Many thanks. ---------------------------------------------------------------------- korba812 · 2014-11-13 17:22 You can use OnShowHint event, eg: Code: [Select]procedure TfrmMainForm.lbDisplayListShowHint(Sender: TObject; HintInfo: PHintInfo); begin   if (HintInfo^.HintControl = lbDisplayList) and (lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True) > -1) then       HintInfo^.HintStr := lbDisplayList.Items[lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True)]; end; ---------------------------------------------------------------------- William Marshall · 2014-11-13 22:58 wp:  Sorry.  I'm sure it's my inexperience and/or slow learning style, but I haven't seen a connection between what I'm trying to do and anything I've seen about TCustomGrid. Mike.Cornflake:  It's encouraging, in an odd way, that a beginner can have the same problem as one with much more experience.  If I do figure this out, I will certainly post it. korba812:  Thanks for the idea, but it made no difference, at least as written.  I'll play with it, though. Thanks to all for your responses. ---------------------------------------------------------------------- wp · 2014-11-13 23:12 Here's a demo showing the requested behavior. Implementing this at application level is a bit clumsy. If there is interest I could put this code into stdCtrls and post a patch to bugtracker. TListbox would get a new property, like "ShowItemHints" or "HintMode" = (hmRegular, hmTruncatedItems, hmBoth) to activate this feature (along with "ShowHints"). I am not quite sure about the regular hints: Currently they show up when the mouse is above a "short" item or above empty space. Maybe this is distracting (of course you could leave "Hint" empty to avoid this). ---------------------------------------------------------------------- Bart · 2014-11-14 09:42 Quote from: wp on November 14, 2014, 12:12:33 amI am not quite sure about the regular hints: Currently they show up when the mouse is above a "short" item or above empty space. Maybe this is distracting (of course you could leave "Hint" empty to avoid this). I would consider doing this like TStringGrid. Create an OnListItemHint event. User can influence the default behaviour there. There should be a property controliing wether or not to use these kind of hints and a property to control the behaviour of such hints. Maybe they can be combinend in an enum like (lvhNoListItemHints, lvhListItemHintsTruncatedOnly, lvhListItemHintsAll) defaulting to lvhNoListItemHints, so default behaviour remains unchanged. If user selects to have ListItemHints then in the code that shows the hint, first call OnListItemHint, then determine where the mouse is. If the mouse is not over a ListItem display the normal hint, if it is over a listitem display the hint for this listitem. Make sure to update Application.Hint as well, otherwise the Application.OnApplication event won't work as expected anymore. (See my latest revision in cellhints in TCustomGrid). Maybe discuss in devel list before committing such thing? Bart ---------------------------------------------------------------------- William Marshall · 2014-11-14 14:20 wp:   It works!  Short items do not show hints, long items do, and adjacent long items each show their own hints - exactly what I wanted.  The only thing I had to change was the line     if (txt <> '') and (w >= R.Right - R.Left) then to     if (txt <> '') and (w + 3 >= R.Right - R.Left) then otherwise items that are only one or two characters wider than the Listbox aren't caught.  (I wonder if the size of the correction depends on the width of the Listbox.)  Thank you very much for your help. ---------------------------------------------------------------------- korba812 · 2014-11-14 19:35 Take a look: http://docs.embarcadero.com/products/rad_studio/delphiAnd cpp2009/HelpUpdate2/EN/html/delphivclwin32/Forms_THintInfo.html There is THintInfo.CursorRect so: Code: [Select]procedure TfrmMainForm.lbDisplayListShowHint(Sender: TObject; HintInfo: PHintInfo); begin   if (HintInfo^.HintControl = lbDisplayList) and (lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True) > -1) then   begin       HintInfo^.HintStr := lbDisplayList.Items[lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True)];       HintInfo^.CursorRect := lbDisplayList.ItemRect(lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True));   end; end; ---------------------------------------------------------------------- William Marshall · 2014-11-14 22:11 korba812:  This works too!  Using HintInfo^.CursorRect made the difference.  You guys are great!  Here is my final code for your method, slightly modified to allow only long items to appear as hints (and to make it a little easier for me to read). Code: [Select]procedure TfrmMainForm.lbDisplayListShowHint(Sender: TObject; HintInfo: PHintInfo); var Index, ItemWidth, BoxWidth : integer;     Item : string; begin   Index := lbDisplayList.ItemAtPos(HintInfo^.CursorPos, True);   if (HintInfo^.HintControl = lbDisplayList) and (Index > -1) then     begin       Item := lbDisplayList.Items[Index];       ItemWidth := Canvas.TextWidth(Item) + 8;    //  - so lines that are "a little bit" too long are considered too long       BoxWidth := lbDisplayList.Width;       if ItemWidth > BoxWidth then         begin           HintInfo^.HintStr := Item;           HintInfo^.CursorRect := lbDisplayList.ItemRect(Index);        end;     end; end; I don't understand why, but the "item length correction factor" (in ItemWidth := Canvas.TextWidth(Item) + 8;) has to be at least 8 here, but needs to be only 3 in wp's code. Thanks again to everyone.  I would never have figured this out on my own. ---------------------------------------------------------------------- wp · 2014-11-14 22:32 @Bart: Since I see now that this functionality is already there (korba812's code runs also in Ubuntu) I think it would be an overkill to add it to TCustomListbox again as I proposed above. Do you agree? The problem could be that this thread will soon be forgotten...