Solved

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

Posted on 2006-11-06
9
321 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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