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
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
You cal also use this approach.
Set the ListBox style to lbOwnerDrawFixed
then
procedure TForm1.ListBox1DrawItem(Co ntrol: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
ListBox1.Canvas.FillRect(R ect);
TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;
ListBox1.Height := ListBox1.ItemHeight*ListBo x1.Items.C ount;
end;
Set the ListBox style to lbOwnerDrawFixed
then
procedure TForm1.ListBox1DrawItem(Co
begin
with ListBox1.Canvas do
begin
ListBox1.Canvas.FillRect(R
TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;
ListBox1.Height := ListBox1.ItemHeight*ListBo
end;
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Sample program: form, listbox1, botton1, botton2,
Just two onclick events
procedure TForm1.Button1Click(Sender
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
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