Marius0188
asked on
Delphi 7 :: Programmatically adjust TListView height to display all items
Dear Experts,
I have an TListView where each item added respresents a file (and the file's associated icon).
All is working great.
As new items are added to the ListView I would like to determine if all items fit the
display size and if not then I want to increase the height of the ListView to display all items without
showing the scrollbars.
Hope this is clear.
Thanks!
I have an TListView where each item added respresents a file (and the file's associated icon).
All is working great.
As new items are added to the ListView I would like to determine if all items fit the
display size and if not then I want to increase the height of the ListView to display all items without
showing the scrollbars.
Hope this is clear.
Thanks!
ASKER
Close but not exactly working.
Everytime I add a new item the listviews height grows even
when not necessary.
Everytime I add a new item the listviews height grows even
when not necessary.
ASKER
Sorry I should of mentioned:
That I need it be:
ListView.ViewStyle := vsSmallIcon
That I need it be:
ListView.ViewStyle := vsSmallIcon
ahhh i see, ok
procedure TForm1.Button2Click(Sender : TObject);
var
newitem: TListitem;
i: integer;
maxw, maxh: integer;
itemrect: TRect;
begin
newitem := ListView1.Items.Add;
newitem.caption := edit1.text;
newitem.ImageIndex := random(3);
maxh := 0;
maxw := 0;
for i := 0 to pred(listview1.items.count ) do
begin
itemrect := ListView1.Items[i].Display Rect(drBou nds);
maxw := max(maxw, itemrect.Left + (itemrect.Right - itemrect.Left));
maxh := max(maxh, itemrect.Top + (itemrect.Bottom - itemrect.Top));
end;
ListView1.height := 6 + maxh;
end;
procedure TForm1.Button2Click(Sender
var
newitem: TListitem;
i: integer;
maxw, maxh: integer;
itemrect: TRect;
begin
newitem := ListView1.Items.Add;
newitem.caption := edit1.text;
newitem.ImageIndex := random(3);
maxh := 0;
maxw := 0;
for i := 0 to pred(listview1.items.count
begin
itemrect := ListView1.Items[i].Display
maxw := max(maxw, itemrect.Left + (itemrect.Right - itemrect.Left));
maxh := max(maxh, itemrect.Top + (itemrect.Bottom - itemrect.Top));
end;
ListView1.height := 6 + maxh;
end;
add "uses math;" for the "max()" function
ASKER
Its better and closer but still.
But still when adding to 2 filenames as items caption then it sqeezes both in "one line" instead of rather checking what is the ListView current Height and determine if it enough else increase the height of the ListView and insert new item in "second row".
Thanks for helping so far :)
But still when adding to 2 filenames as items caption then it sqeezes both in "one line" instead of rather checking what is the ListView current Height and determine if it enough else increase the height of the ListView and insert new item in "second row".
Thanks for helping so far :)
I'm sorry, i don't quite understand. do you have some example code or can you explain it a little more to me thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have tried your last suggestion.
Looks like the one I need.
So far so good.
Wil let you know as soon I have implement it.
Thanks!
Looks like the one I need.
So far so good.
Wil let you know as soon I have implement it.
Thanks!
procedure TForm1.Button1Click(Sender
var
newitem: TListitem;
requiredsize: integer;
tm: tagTEXTMETRIC;
newcount: integer;
begin
ListView1.ViewStyle := vsreport;
newcount := ListView1.Items.Count + 1; // we are going to add an item
GetTextMetrics(Listview1.C
requiredsize := (newcount) * (tm.tmHeight + 1);
requiredsize := 12 + tm.tmHeight + requiredsize; // room for border and header + (height per line)
listview1.height := requiredsize;
// now add the item - adding after resizing so that scrollbars dont try to appear
newitem := ListView1.Items.Add;
newitem.caption := edit1.text;
end;