Board index » cppbuilder » TListView Again

TListView Again


2003-07-22 04:52:07 PM
cppbuilder38
I'm trying to use that ListView component but I don't know how it is
constructed I tried to understand it from the example passed from "Rodolfo
Frino", but I still need to access every individual item through its index ,
is this available ???
and if it is how to ??
thanks in advance
 
 

Re:TListView Again

Quote
I'm trying to use that ListView component but I don't know how it is
constructed I tried to understand it from the example passed from "Rodolfo
Frino", but I still need to access every individual item through its index ,
is this available ???
and if it is how to ??

More or less...
Say the listview is vsReport (columnar list), and has 3 columns total. The
listview is a collection of list items. Each list item has a caption property,
and then 2 string properties. The first column is the caption property, the
second column is the first string property, and the third column is the second
string property.
Here's some examples:
// clear the listview
ListView1->Items->Clear();
// add a row to the listview
TListItem *MyItem=ListView1->Items->Add(); // create the entry
MyItem->Caption="Caption for line";
MyItem->SubItems->Add("Column 2 data");
MyItem->SubItems->Add("Column 3 data");
// now scan the listview
for (int i=0;i<ListView1->Items->Count;++i)
{
Edit1->Text=ListView1->Items->Item[i]->Caption; // fetch row caption (column
1)
Edit2->Text=ListView1->Items->Item[i]->SubItems->Strings[0]; // fetch first
string (column 2);
TListItem *MyXItem=ListView1->Items->Item[i];
Edit3->Text=MyXItem->SubItems->Strings[1]; // fetch second string (Column 3);
}
// and if you wish to use a line selected when RowSelect==true...
if (ListView1->Selected)
{
Edit1->Text=ListView1->Selected->Caption; // fetch row caption (column 1)
TListItem *MyYItem=ListView1->Selected;
Edit3->Text=MyYItem->SubItems->Strings[1]; // fetch second string (Column 3);
}
If you would like to access a vsReport listview by cell number:
//---------------------------------------------------------------------------
void __fastcall TForm1::SetLVCell(int row,int column,String Data)
{
TListItem *MyItem;
// assume row and column are both 0-based
if (row>=ListView1->Items->Count)
{
// fill listview up to row number with blanks
do
{
MyItem=ListView1->Items->Add();
MyItem->Caption="";
for (int i=0;i<ListView1->Columns->Count-1;++i)
MyItem->SubItems->Add("");
}while(ListView1->Items->Count<=row);
}
MyItem=ListView1->Items->Item[row];
if (column==0)
ListView1->Items->Item[row]->Caption=Data;
else
ListView1->Items->Item[row]->SubItems->Strings[column-1]=Data;
Edit2->Text=ListView1->Items->Count;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (CSpinEdit2->Value>=ListView1->Columns->Count)
ShowMessage("Column too big");
else
SetLVCell(CSpinEdit1->Value,CSpinEdit2->Value,Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListView1->Items->Clear();
Edit2->Text=ListView1->Items->Count;
}
keith
 

Re:TListView Again

thanks Keith I will try it.
"Keith" < XXXX@XXXXX.COM >wrote in message
Quote
>I'm trying to use that ListView component but I don't know how it is
>constructed I tried to understand it from the example passed from
"Rodolfo
>Frino", but I still need to access every individual item through its
index ,
>is this available ???
>and if it is how to ??
>

More or less...

Say the listview is vsReport (columnar list), and has 3 columns total.
The
listview is a collection of list items. Each list item has a caption
property,
and then 2 string properties. The first column is the caption property,
the
second column is the first string property, and the third column is the
second
string property.

Here's some examples:

// clear the listview
ListView1->Items->Clear();

// add a row to the listview
TListItem *MyItem=ListView1->Items->Add(); // create the entry
MyItem->Caption="Caption for line";
MyItem->SubItems->Add("Column 2 data");
MyItem->SubItems->Add("Column 3 data");

// now scan the listview
for (int i=0;i<ListView1->Items->Count;++i)
{
Edit1->Text=ListView1->Items->Item[i]->Caption; // fetch row caption
(column
1)
Edit2->Text=ListView1->Items->Item[i]->SubItems->Strings[0]; // fetch
first
string (column 2);
TListItem *MyXItem=ListView1->Items->Item[i];
Edit3->Text=MyXItem->SubItems->Strings[1]; // fetch second string
(Column 3);
}

// and if you wish to use a line selected when RowSelect==true...
if (ListView1->Selected)
{
Edit1->Text=ListView1->Selected->Caption; // fetch row caption (column
1)
TListItem *MyYItem=ListView1->Selected;
Edit3->Text=MyYItem->SubItems->Strings[1]; // fetch second string
(Column 3);
}

If you would like to access a vsReport listview by cell number:

file://---------------------------------------------------------------------
------
Quote
void __fastcall TForm1::SetLVCell(int row,int column,String Data)
{
TListItem *MyItem;

// assume row and column are both 0-based
if (row>=ListView1->Items->Count)
{
// fill listview up to row number with blanks
do
{
MyItem=ListView1->Items->Add();
MyItem->Caption="";
for (int i=0;i<ListView1->Columns->Count-1;++i)
MyItem->SubItems->Add("");
}while(ListView1->Items->Count<=row);
}

MyItem=ListView1->Items->Item[row];

if (column==0)
ListView1->Items->Item[row]->Caption=Data;
else
ListView1->Items->Item[row]->SubItems->Strings[column-1]=Data;

Edit2->Text=ListView1->Items->Count;
}

file://---------------------------------------------------------------------
------
Quote
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (CSpinEdit2->Value>=ListView1->Columns->Count)
ShowMessage("Column too big");
else
SetLVCell(CSpinEdit1->Value,CSpinEdit2->Value,Edit1->Text);
}

file://---------------------------------------------------------------------
------
Quote
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListView1->Items->Clear();
Edit2->Text=ListView1->Items->Count;
}


keith

 

{smallsort}