Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

TListview height and width

Posted on 2005-04-16
9
Medium Priority
?
1,517 Views
Last Modified: 2012-08-13
Hi

I putting a TListview inside a scrollbox and wants to set its width and
height so no scrollbars are displayed in the listview, but only on the
scrollbox?

How can I get the width and height that the listview would require to stop
showing the scrollbars?
I don't suppose a listview has an autosize function like TImage?
0
Comment
Question by:koger
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 13797076
    Hi!

Simply put this in
TForm1.Create(..)
..
ListView1.Width := <desired width>
 ListView1.Heigth := <desired heigth>
ListView1.Top := ScrollBox1.Top;
ListView1.Left := ScrollBox1.Left;
..

Regards,
  Tomas Helgi
0
 

Author Comment

by:koger
ID: 13797085
I don't know the desired width and height, that's what I want to determine!
0
 
LVL 1

Accepted Solution

by:
JustinWillis earned 200 total points
ID: 13797096
Hello, please see the following example, it seems to work great for me, BUT you may need to tweak it depending on your requirements, it assumes the default listitem height of 18, the width's are detected, this uses a scrollbox containing a listview, items are added OnCreate of form, clicking Button1 resizes the ListView depending on contents causing only the ScollBox scrollbar's to show...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    ListView1: TListView;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);

const
  Names: array[0..5, 0..1] of string = (
    ('Rubble', 'Barney'),
    ('Michael', 'Johnson'),
    ('Bunny', 'Bugs'),
    ('Silver', 'HiHo'),
    ('Simpson', 'Bart'),
    ('Squirrel', 'Rockey')
    );

var
  I: Integer;
  NewColumn: TListColumn;
  ListItem: TListItem;
begin
  with ListView1 do
  begin
    ViewStyle := vsReport;

    NewColumn := Columns.Add;
    NewColumn.Caption := 'Last';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'First';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'Third';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'Fourth';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'Fifth';

    for I := Low(Names) to High(Names) do
    begin
      ListItem := Items.Add;
      ListItem.Caption := Names[I][0];
      ListItem.SubItems.Add(Names[I][1]);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
    NewWidth:Integer;
begin
        with listview1 do
        begin
        Height := (Items.Count * 18);
        end;

        NewWidth := 0;

        with listview1 do
        begin
        for I := 0 to columns.Count - 1 do
         begin
         NewWidth := NewWidth + Columns.Items[I].Width;
         end;
        Width := NewWidth;
        end;

end;

end.

Hope this helps!
Regards,
Justin Willis.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:JustinWillis
ID: 13797106
Oh forgot to mention obviously you will need to make the ScrollBox and the ListView rather small to test this example as not much content is provided in the test ;) but should work just as well with large amounts of items/columns.

Please note also that the listview left/top needs to be at 0 to get the height and width's right, although if this is not desired then you can tweak the code to get a rough guess.

Hope that makes sense.
Just.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13797861
Why dont you want to use scrollbars for the listview and only scrollbox ones?
If you align the listview to alclient within the scrollbox, you only get one set of scrollbars anyway.
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13797891
That's what I was thinking, but hopefully there is a reason? right Koger? otherwise that isn't quite as much fun ;)

J.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13800340
he he ... in fact if you have no scrollbox at all ... you STILL only get one set of scrollbars :o)
Seems like a strange design idea to me.
0
 

Author Comment

by:koger
ID: 13804516
I found a better way

Add the CommCtrl unit to your Uses clause (in the Implementation section).
Try this:

var
  R: TSmallpoint;
begin
  R:= TSmallpoint(         ListView_ApproximateViewRect(
         Listview1.Handle,Word(-1),Word(-1),-1)
         );
  Listview1.ClientWidth := R.X;
  Listview1.ClientHeight:= R.Y;
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13804564
Interesting, thanks.
J
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 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