Solved

Delphi 7 :: Programmatically adjust TListView height to display all items

Posted on 2006-11-06
9
302 Views
Last Modified: 2010-04-04
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!
0
Comment
Question by:Marius0188
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17886528
something like this?

procedure TForm1.Button1Click(Sender: TObject);
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.Canvas.Handle, tm);
  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;
0
 

Author Comment

by:Marius0188
ID: 17886648
Close but not exactly working.

Everytime I add a new item the listviews height grows even
when not necessary.

0
 

Author Comment

by:Marius0188
ID: 17886665
Sorry I should of mentioned:

That I need it be:

ListView.ViewStyle := vsSmallIcon
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17886792
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].DisplayRect(drBounds);
      maxw := max(maxw, itemrect.Left + (itemrect.Right - itemrect.Left));
      maxh := max(maxh, itemrect.Top + (itemrect.Bottom - itemrect.Top));
    end;
    ListView1.height := 6 + maxh;
end;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17886795
add "uses math;" for the "max()" function
0
 

Author Comment

by:Marius0188
ID: 17890532
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 :)
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17894503
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.
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 17894769
actually, I think I know what you mean. Try this one

procedure TForm1.Button3Click(Sender: TObject);
var
  newitem: TListitem;
begin
  newitem := ListView1.Items.Add;
  inc(uniquenumber);
  newitem.caption := edit1.text;
  RearrangeListViewIconsAndHeight;
end;

procedure TForm1.RearrangeListViewIconsAndHeight;
var
  newitem: TListitem;
  i: integer;
  maxw, maxh: integer;
  itemrect: TRect;
  newpos: tpoint;
  currentx, currenty: integer;
  itemsinthisrow: integer;
  lvdisplaywidth: integer;
begin
    maxh := 0;
    maxw := 0;
    lvdisplaywidth := Listview1.Width - 2;
    itemsinthisrow := 0;
    currentx := 0;
    currenty := 0;
    for i := 0 to pred(listview1.items.count) do
    begin
      itemrect := ListView1.Items[i].DisplayRect(drBounds);
      if (currentx + (itemrect.Right - itemrect.Left)) > lvdisplaywidth then
      begin  // goes outide of bounds, move down if possible
        currentx := 0;
        newpos.x := 0;
        currenty := currenty + (itemrect.Bottom - itemrect.Top); // assume same "height" for all icons
        newpos.y := currenty;
        itemsinthisrow := 0;
      end
      else
      begin
        inc(itemsinthisrow);
        newpos.x := currentx;
        newpos.y := currenty;
      end;
      ListView1.Items[i].SetPosition(newpos);
      currentx := currentx + (itemrect.Right - itemrect.Left);
      maxw := max(maxw, currentx);
      maxh := max(maxh, currenty + (itemrect.Bottom - itemrect.Top));
    end;
    ListView1.height := 6 + maxh;
end;
0
 

Author Comment

by:Marius0188
ID: 17906587
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now