Solved

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

Posted on 2006-11-06
9
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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