MicroTip#4 const Args: array of … 的應用

來源:互聯網
上載者:User
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;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.