Recent

Author Topic: List box item height  (Read 700 times)

tailkinker

  • Newbie
  • Posts: 6
List box item height
« on: June 14, 2022, 12:37:18 pm »
So the TListbox.ItemHeight documentation states that this is not implemented.  How can I determine the height of each item in the TListBox?  (I want to make the TListBox automatically scale with items, so I never need to worry about scroll bars.)

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: List box item height
« Reply #1 on: June 14, 2022, 01:05:12 pm »
Set the Style to lbOwnerDrawFixed, and you can set ItemHeight to any value. Or set Style to lbOwnerDrawVariable, and you can define an individual height for each line by implementing a handler for the OnMeasureItem event.

tailkinker

  • Newbie
  • Posts: 6
Re: List box item height
« Reply #2 on: June 14, 2022, 01:17:58 pm »
The problem is that I want to determine the height as set by the font being used, rather than trying to guess.  (Which is what I am currently doing.) 

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: List box item height
« Reply #3 on: June 14, 2022, 01:50:13 pm »
The following code yields the same line height as that of a listbox with lbStandard style. --> Just measure the height of some text in the listbox (you could use the Listbox.canvas directly, but since that is not always available I used an auxiliary bitmap):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4.   h: Integer;
  5. begin
  6.   bmp := TBitmap.Create;
  7.   try
  8.     bmp.SetSize(1, 1);
  9.     bmp.Canvas.Font := Listbox2.Font;
  10.     h := bmp.Canvas.TextHeight('Tg');
  11.   finally
  12.     bmp.Free;
  13.   end;
  14.   Listbox2.ItemHeight := h;
  15. end;

tailkinker

  • Newbie
  • Posts: 6
Re: List box item height
« Reply #4 on: June 14, 2022, 02:06:42 pm »
Unfortunately, this does not work.  The size returned does not in fact equal the height in pixels of the text when displayed in the listbox.

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: List box item height
« Reply #5 on: June 14, 2022, 02:53:53 pm »
I am on Windows, and the code is working (see also the attached project). But since the listbox is controlled by the widgetset this may not be true for other operating systems. Just run the attached demo and adjust the "extra pixels" (added to the pure "char height") so that the line heights of both listboxes are the same (the left one has Style lbStandard).

tailkinker

  • Newbie
  • Posts: 6
Re: List box item height
« Reply #6 on: June 14, 2022, 05:48:38 pm »
Well, that works, but it means mucking with the source every time I change environments.  I think I'll look at using a different control.  Thanks to all those that helped.

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: List box item height
« Reply #7 on: June 14, 2022, 07:18:18 pm »
Ah, there's a simpler way: Query the rectangle occupied by an item:
Code: Pascal  [Select][+][-]
  1. var
  2.   R: TRect;
  3. ...
  4.   R := AListbox.ItemRect(0);
  5.   height := R.Bottom - R.Top;
  6.  
« Last Edit: June 14, 2022, 07:21:52 pm by wp »

 

TinyPortal © 2005-2018