Avatar of boycoder
boycoder

asked on 

auto extend a listbox

Hi i have a small list box, how can i extend the size of the hieght based on entries?

so when 3 items are added for example its the exact size of the 3, same for 2 and all the way up to 10, then it starts to show the scroller?

Thanks
DelphiPascal

Avatar of undefined
Last Comment
kumaresan2011
Avatar of Thomas_Roes
Thomas_Roes

Presumably the number of items is changed at runtime. I don't know how or when you do this, but after you change the number of items in the listbox's itemlist, check the number of items and ajust the height of the listbox.

Sample program: form, listbox1, botton1, botton2,

Just two onclick events



procedure TForm1.Button1Click(Sender: TObject);
begin
  listbox1.Items.Add('test');
  if listbox1.Items.Count > 10 then
    listbox1.Height := 10 * listbox1.ItemHeight + 5
  else
    listbox1.Height := listbox1.Items.Count * listbox1.ItemHeight + 5;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  listbox1.Items.Delete(1);
  if listbox1.Items.Count > 10 then
    listbox1.Height := 10 * listbox1.ItemHeight + 5
  else
    listbox1.Height := listbox1.Items.Count * listbox1.ItemHeight + 5;
end;

Simple but effective
Avatar of Ferruccio Accalai
You cal also use this approach.
Set the ListBox style to lbOwnerDrawFixed

then

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  with ListBox1.Canvas do
  begin
    ListBox1.Canvas.FillRect(Rect);
    TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
  end;
  ListBox1.Height := ListBox1.ItemHeight*ListBox1.Items.Count;
end;
ASKER CERTIFIED SOLUTION
Avatar of kumaresan2011
kumaresan2011
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo