Microtip #4 const ARGs: Application of array...
// Test in DELPHI6 up2
// Wrtten by skyjacker 2007.03.21
// QQ discuss group: 130970
// Note: The Source Code released by me is for reference only and there is no bug.
// You are welcome to discuss code or bugs.
Application requirements: Use a function to add a row of data to tlistview.
Feature: the number of columns is not fixed.
Therefore, using the open array parameter const ARGs: array of is a good method.
Const ARGs: array of has two forms: fixed type and variable type.
The following two functions are implemented:
Procedure listaddline (const ARGs: array of string; alistview: tlistview );
{* Add a row to the list}
Procedure listaddlinet (const ARGs: array of const; alistview: tlistview );
{* Add a row to the list, multi-type edition}
Usage:
Listaddcolumn ('test 1', lvdata );
Listaddcolumn ('test 2', lvdata );
Listaddcolumn ('test 3', lvdata );
Listaddline ([], lvdata );
Listaddline (['1'], lvdata );
Listaddline (['1', '2'], lvdata );
Listaddline (['1', '2', '3'], lvdata );
Listaddlinet ([], lvdata );
Listaddlinet (['2'], lvdata );
Listaddlinet (['2', 22.2], lvdata );
Listaddlinet (['2', 33, 3.01], lvdata );
Source:
// Add a column header to the list
Procedure listaddcolumn (const acolumncpation: string; alistview: tlistview );
Begin
If assigned (alistview) then
Alistview. Columns. Add. Caption: = acolumncpation;
End;
// Add a row to the list
Procedure listaddline (const ARGs: array of string; alistview: tlistview );
VaR
I: integer;
Begin
If high (ARGs) <0 then
Exit;
With alistview. Items. Add do
Begin
Caption: = ARGs [0];
For I: = low (ARGs) + 1 to high (ARGs) Do
Begin
Subitems. Add (ARGs [I]);
End;
End;
End;
// Add a row to the list, multi-type edition
Procedure listaddlinet (const ARGs: array of const; alistview: tlistview );
VaR
I: integer;
S: string;
Begin
With alistview. Items. Add do
Begin
For I: = low (ARGs) to high (ARGs) Do
Begin
Case ARGs [I]. vtype
Vtinteger:
S: = inttostr (ARGs [I]. vinteger );
Vtboolean:
If ARGs [I]. vboolean then
S: = '1'
Else
S: = '0 ';
Vtchar:
S: = ARGs [I]. vchar;
Vtextended:
S: = floattostr (ARGs [I]. vextended ^ );
Vtstring, vtansistring:
S: = ARGs [I]. vstring ^;
Vtwidechar:
S: = ARGs [I]. vwidechar;
Else
S: = 'unknown type ';
End;
If I = 0 then
Caption: = s
Else
Subitems. Add (s );
End;
End;
End;