Solved

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

Posted on 2006-11-06
9
300 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

10 Experts available now in Live!

Get 1:1 Help Now