Board index » cppbuilder » Autosizing TListColumn in a TListView

Autosizing TListColumn in a TListView

Hi,

    I am trying to resize TListColumn items. The first time it works perfectly, but any time afther it not. I do use
ListView1->Columns->Clear() frequently. The code is listed below. Can you see an error in it? Thanks in advance.

In Unit1.h:
(as public members of the class)
  TListColumn *NewColumn;
  TListItem  *ListItem;
  TListView   *ListView;

In Unit1.cpp:
const char Names[6][2][10] =
   {{"Rubble","Barny"},
    {"Michael", "Johnson"},
    {"Bunny", "Bugs"},
    {"Silver", "HiHo"},
    {"Simpson", "Bart"},
    {"Squirrel", "Rocky"}};

// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{

  ListView1->Parent = this;
  ListView1->Align = alTop;
  ListView1->ViewStyle = vsReport;

  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "Last";
  NewColumn->AutoSize = true;

  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "First";
  NewColumn->AutoSize = true;

  for (int i = 0; i < 6; i++)
  {
    ListItem = ListView1->Items->Add();
    ListItem->Caption = Names[i][0];
    ListItem->SubItems->Add(Names[i][1]);
  }

Quote
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ListView1->Columns->Clear();
  ListView1->Items->Clear();

  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "Last";
  NewColumn->AutoSize = true;

  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "First";
  NewColumn->AutoSize = true;

  for (int i = 0; i < 6; i++)
  {
    ListItem = ListView1->Items->Add();
    ListItem->Caption = Names[i][0];
    ListItem->SubItems->Add(Names[i][1]);
  }

Quote
}

// ---------------------------------------------------------------------------

Greetings,
Ramses

 

Re:Autosizing TListColumn in a TListView


Quote
>     I am trying to resize TListColumn items. The first time it works
> perfectly, but any time afther it not.

Actually, it doesn't work at all.  The first time, it evenly distributes
the columns (50% of the ListView's width).  The rest of the time it just
sets their width to 50 pixels.  And yet, the help file states that
AutoSize makes the column "size itself to the width of its text".  Three
universally different behaviours: Go figure!

I'm not sure you'll want to rely on this property.  You'll be better
using TListColumn::Width and calculating the values on your own.  (By
the way, Width can be set to some predefined values such as -1, which
makes the column fit the text or -2 to fit the header.  Check out the
help files for more info.)

Good luck!

--
Yoto Yotov

Other Threads