Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-06
9
Medium Priority
?
422 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

782 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