Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:41+00:00 2026-05-30T02:05:41+00:00

When I use a TListView (ViewStyle = vsReport) I can autofit the width of

  • 0

When I use a TListView (ViewStyle = vsReport) I can autofit the width of the columns setting the LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER values in the Width property of each column, now I start to use the Listview in virtual mode, but the width of the columns is not modified according to these values. So the question is : How I can adjust the width of the columns to fit to the content or header, when the lisvtiew is in virtual mode?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:05:42+00:00Added an answer on May 30, 2026 at 2:05 am

    Since the list view in virtual mode don’t know the item captions in advance (because it asks only for data of the visible area) it also cannot know the width of the widest one, so that’s the reason why the autosize flag of the LVM_SETCOLUMNWIDTH behaves this way.

    Thus the only way is to write a custom function which will query all your data, measure the text widths of all future captions and set the column width to the value of the widest one.

    The following example shows how to do it. It uses the ListView_GetStringWidth macro for the text width calculations (it seems to be the most natural way to do this). However the problem is the value of the text padding. As it’s stated in the documentation:

    The ListView_GetStringWidth macro returns the exact width, in pixels,
    of the specified string. If you use the returned string width as the
    column width in a call to the ListView_SetColumnWidth macro, the
    string will be truncated. To retrieve the column width that can
    contain the string without truncating it, you must add padding to the
    returned string width.

    But they didn’t mention there how to get the padding value (and seems they won't to do so). Some people say (e.g. here) that it’s enough to use 6 px for item’s padding and 12 px for subitem’s padding, but it isn’t (at least for this example on Windows 7).

    ///////////////////////////////////////////////////////////////////////////////
    /////   List View Column Autosize (Virtual Mode)   ////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, StdCtrls,
      Forms, Dialogs, StrUtils, ComCtrls, CommCtrl;
    
    type
      TSampleRecord = record
        Column1: string;
        Column2: string;
        Column3: string;
      end;
      TSampleArray = array [0..49] of TSampleRecord;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        ListView1: TListView;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        SampleArray: TSampleArray;
        procedure AutoResizeColumn(const AListView: TListView;
          const AColumn: Integer);
        procedure OnListViewData(Sender: TObject; Item: TListItem);
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   TForm1.AutoResizeColumn - auto-size column   //////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    // AListView - list view object instance
    // AColumn - index of the column to be auto-sized
    
    procedure TForm1.AutoResizeColumn(const AListView: TListView;
      const AColumn: Integer);
    var
      S: string;
      I: Integer;
      MaxWidth: Integer;
      ItemWidth: Integer;
    begin
      // set the destination column width to the column's caption width
      // later on we'll check if we have a wider item
      MaxWidth := ListView_GetStringWidth(AListView.Handle,
        PChar(AListView.Columns.Items[AColumn].Caption));
      // iterate through all data items and check if their captions are
      // wider than the currently widest item if so then store that value
      for I := 0 to High(SampleArray) do
      begin
        case AColumn of
          0: S := SampleArray[I].Column1;
          1: S := SampleArray[I].Column2;
          2: S := SampleArray[I].Column3;
        end;
        ItemWidth := ListView_GetStringWidth(AListView.Handle, PChar(S));
        if MaxWidth < ItemWidth then
          MaxWidth := ItemWidth;
      end;
      // here is hard to say what value to use for padding to prevent the
      // string to be truncated; I've found the suggestions to use 6 px
      // for item caption padding and 12 px for subitem caption padding,
      // but a few quick tests confirmed me to use at least 7 px for items
      // and 14 px for subitems
      if AColumn = 0 then
        MaxWidth := MaxWidth + 7
      else
        MaxWidth := MaxWidth + 14;
      // and here we set the column width with caption padding included
      AListView.Columns.Items[AColumn].Width := MaxWidth;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   TForm1.FormCreate - setup the list view and fill custom data   ////////
    ///////////////////////////////////////////////////////////////////////////////
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
    begin
      ListView1.ViewStyle := vsReport;
      ListView1.Columns.Add.Caption := 'Column 1';
      ListView1.Columns.Add.Caption := 'Column 2';
      ListView1.Columns.Add.Caption := 'Column 3';
      ListView1.OwnerData := True;
      ListView1.OnData := OnListViewData;
      ListView1.Items.Count := High(SampleArray);
    
      for I := 0 to High(SampleArray) do
      begin
        SampleArray[I].Column1 := 'Cell [0, ' + IntToStr(I) + '] ' +
          DupeString('x', I);
        SampleArray[I].Column2 := 'Cell [1, ' + IntToStr(I) + '] ' +
          DupeString('x', High(SampleArray) - I);
        SampleArray[I].Column3 := '';
      end;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   TForm1.FormCreate - custom handler for OnData event   /////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    procedure TForm1.OnListViewData(Sender: TObject; Item: TListItem);
    begin
      Item.Caption := SampleArray[Item.Index].Column1;
      Item.SubItems.Add(SampleArray[Item.Index].Column2);
      Item.SubItems.Add(SampleArray[Item.Index].Column3);
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   TForm1.Button1Click - auto-resize all 3 columns   /////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      AutoResizeColumn(ListView1, 0);
      AutoResizeColumn(ListView1, 1);
      AutoResizeColumn(ListView1, 2);
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Listview has a data-inset property to make its corners rounded. However, I can't use
I'm trying to add a column between existing columns in a TListView. Therefor I
In WPF, I know I can use ListView.ScrollIntoView to scroll a particular item into
How can I use the ListView_GetBkImage macro: http://msdn.microsoft.com/en-us/library/bb761246(v=VS.85).aspx ... from a C#/WinForms application? I
I have a listview with 200 items. I use a custom view for each
I tried to use a TListView component to display rather large data lists (like
I use listview but prefer the column size to be fixed. Have no idea
Hi how i can use custom listview with textview and imageview.i tried following code
I need to draw a checkbox in a particular column in a TListView ,
I use ListView with link like this: <ul data-dividertheme=d data-theme=c data-role=listview style=width:100%;margin: 0px >

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.