CnPack Forum


 
Subject: MicroTip#4 const Args: array of ... 的应用
skyjacker
版主
Rank: 7Rank: 7Rank: 7
茶农


UID 2239
Digest Posts 9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
Post at 2007-3-21 10:36  Profile | Blog | P.M.  | QQ
MicroTip#4 const Args: array of ... 的应用

MicroTip#4 const Args: array of ... 的应用

// Test in Delphi6 up2
// Wrtten by SkyJacker 2007.03.21
// QQ Discuss Group: 130970

// 注:本人发布的源码,仅供参考,不保证无Bug。
// 非常欢迎讨论 Code 或 Bug 问题。

应用要求:用一个函数实现对 TListView 添加一行数据。
特点:列的个数不固定。

因此,用开放数组参数 const Args: array of  是个不错的办法。
const Args: array of 的形式有两种: 固定类型和可变类型。

实现了如下两个函数:
procedure ListAddLine(const Args: array of string; AListView: TListView);
{* 在列表中增加一行}
procedure ListAddLineT(const Args: array of const; AListView: TListView);
{* 在列表中增加一行, 多类型版}

使用方法:
  ListAddColumn('测试1', lvData);
  ListAddColumn('测试2', lvData);
  ListAddColumn('测试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:

// 在列表中增加一列头
procedure ListAddColumn(const AColumnCpation: string; AListView: TListView);
begin
  if Assigned(AListView) then
    AListView.Columns.Add.Caption := AColumnCpation;
end;

// 在列表中增加一行
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;

// 在列表中增加一行, 多类型版
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 of
        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;

[ 本帖最后由 skyjacker 于 2007-3-21 10:38 编辑 ]




一壶清茶煮青春.
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6760
Posts 3556
点点分 6760
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2007-3-21 12:32  Profile | Blog | P.M. 
Skyjacker的tips越来越多,值得庆贺表扬。

提个小建议:貌似代码太多,说明性的文字太少,这样会导致主题不够鲜明。

主题鲜明表述清楚了才能放入俺们的文档中心啊。
Top
skyjacker
版主
Rank: 7Rank: 7Rank: 7
茶农


UID 2239
Digest Posts 9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
Post at 2007-3-21 13:02  Profile | Blog | P.M.  | QQ
好。
需要继续努力。
下次规范些。
让自己和大家明明白白的




一壶清茶煮青春.
Top
kendling (小冬)
高级版主
Rank: 8Rank: 8
MyvNet


Medal No.1  
UID 703
Digest Posts 5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
Post at 2007-3-21 15:55  Profile | Site | Blog | P.M.  | QQ | Yahoo!
哈,支持支持,,,




小冬
http://MyvNet.com
Top
zzzl (早安的空气)
版主
Rank: 7Rank: 7Rank: 7



UID 590
Digest Posts 0
Credits 399
Posts 199
点点分 399
Reading Access 100
Registered 2004-11-29
Status Offline
Post at 2007-3-22 08:53  Profile | Blog | P.M.  | QQ
竟然不保证无bug
Top
 




All times are GMT++8, the time now is 2024-5-1 08:41

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.006655 second(s), 7 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP