Page 1 of 1

ListView

PostPosted: Mon Apr 16, 2018 12:50 pm
by mark_c
I've a ListView with many rows and columns and the code is similar as you can see below. The pourpose is writing in every fileds of the ListView but, how can you see, if I've a ListView with 1000 fileds, I think is is wrong write a long list of the fix1->u1; ..... fix1->u1000;

Which is the best solution?

Thankyou


Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

struct TFileItem {
String u1;
String u2;
String u3;
String u4;
String u5;
String u6;
String u7;
String u8;
};
TList *FileList;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        ListView1->OwnerData = true;
        ListView1->ViewStyle=vsReport;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        TFileItem* fi = new TFileItem;
        FileList = new TList;
        ListView1->Items->Count=0;

        for(int x=0; x < 100; x++)
        {
                fi->u1="filed1";
                fi->u2="filed2";
                fi->u3="filed3";
                fi->u4="filed4";
                //fi->???
               
        FileList->Add(fi);
        }

        ListView1->Items->Count = FileList->Count;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView1Data(TObject *Sender, TListItem *Item)
{
        TFileItem* fi = (TFileItem*)FileList->Items[Item->Index];
        Item->Caption = Item->Index+1;

        Item->SubItems->Add(fi->u1);
        Item->SubItems->Add(fi->u2);
        Item->SubItems->Add(fi->u3);
        Item->SubItems->Add(fi->u4);
        //Item->SubItems->Add(fi->u...);
}
//---------------------------------------------------------------------------

Re: ListView

PostPosted: Mon Apr 16, 2018 1:59 pm
by rlebeau
mark_c wrote:how can you see, if I've a ListView with 1000 fileds, I think is is wrong write a long list of the fix1->u1; ..... fix1->u1000;

Which is the best solution?


You can use an array instead of individual fields, eg:

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

struct TFileItem {
    String u[1000]; // <-- HERE!
};
TList *FileList;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    // NOTE: you can/should set these properties at design-time instead...
    ListView1->OwnerData = true;
    ListView1->ViewStyle = vsReport;

    FileList = new TList;
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
    ClearFileList();
    delete FileList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearFileList()
{
    for (int x = 0; x < FileList->Count; ++x)
        delete (TFileItem*) FileList->Items[x];
    FileList->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ListView1->Items->Count = 0;
    ClearFileList();

    for(int x = 0; x < 100; ++x)
    {
        TFileItem* fi = new TFileItem;
        for (int y = 0; y < 1000; ++y)
            fi->u[y] = _D("filed") + String(y+1);
        FileList->Add(fi);
    }

    ListView1->Items->Count = FileList->Count;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView1Data(TObject *Sender, TListItem *Item)
{
    TFileItem* fi = (TFileItem*) FileList->Items[Item->Index];
    Item->Caption = Item->Index+1;

    Item->SubItems->BeginUpdate();
    try
    {
        for (int x = 0; x < 1000; ++x)
            Item->SubItems->Add(fi->u[x]);
    }
    __finally
    {
        Item->SubItems->EndUpdate();
    }
}
//---------------------------------------------------------------------------