Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
LCL
»
List box item height
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Printer Paper Orientation
by
paweld
[
Today
at 12:15:03 pm]
FCL streamex question
by
marcou
[
Today
at 12:09:59 pm]
Does Freepascal has every...
by
y.ivanov
[
Today
at 12:04:04 pm]
Turbo Power Flashfiler La...
by
zzsczz
[
Today
at 11:33:58 am]
Can FreePascal be used to...
by
MarkMLl
[
Today
at 11:24:04 am]
Reading pdf into a TMemo
by
wp
[
Today
at 11:18:03 am]
IBExpress - best practise...
by
Nicole
[
Today
at 10:09:46 am]
Inserting into TValueList...
by
derek.john.evans
[
Today
at 10:02:22 am]
Question about cross-comp...
by
PascalDragon
[
Today
at 09:10:39 am]
[SOLVED] Type conversion?...
by
PascalDragon
[
Today
at 09:06:01 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: List box item height (Read 388 times)
tailkinker
New member
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.)
Logged
wp
Hero Member
Posts: 9921
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.
Logged
tailkinker
New member
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.)
Logged
wp
Hero Member
Posts: 9921
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
var
bmp
:
TBitmap
;
h
:
Integer
;
begin
bmp
:
=
TBitmap
.
Create
;
try
bmp
.
SetSize
(
1
,
1
)
;
bmp
.
Canvas
.
Font
:
=
Listbox2
.
Font
;
h
:
=
bmp
.
Canvas
.
TextHeight
(
'Tg'
)
;
finally
bmp
.
Free
;
end
;
Listbox2
.
ItemHeight
:
=
h
;
end
;
Logged
tailkinker
New member
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.
Logged
wp
Hero Member
Posts: 9921
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).
Logged
tailkinker
New member
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.
Logged
wp
Hero Member
Posts: 9921
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]
[+]
[-]
var
R
:
TRect
;
...
R
:
=
AListbox
.
ItemRect
(
0
)
;
height
:
=
R
.
Bottom
-
R
.
Top
;
«
Last Edit: June 14, 2022, 07:21:52 pm by wp
»
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
LCL
»
List box item height
TinyPortal
© 2005-2018