www.pudn.com > ChaoJiQiDong.rar > u_ListView.pas, change:2009-11-22,size:3117b


unit u_ListView; 
 
interface 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,ComCtrls; 
 
 
 
  procedure ListView_AddItems(ListView:TListView;Params:array of string);   //添加行 
  procedure ListView_AddColumus(ListView:TListView;ColumnsList:TStringList);  //添加列表头 
  Function ListView_ModifyListViewByCaption(ListView:TListView;ListIndex:Integer; 
      ColumnsText:string;AddStr:string):string; 
  procedure ListView_MondifyListSequenceNumber(ListView:TListView);  //修正列表序号 
implementation 
 
 
Function GetStr(S:String;Str1:String;Str2:String):String; 
var temp:string; 
begin 
  Result:=''; 
  If (Pos(Str1,S)=0) and (Pos(Str2,S)=0) Then Exit; 
  If (Str1='') and (Pos(Str2,S)<>0) then 
  begin 
    Result:=Trim(Copy(S,0,Pos(Str2,S)-1)); 
    Exit; 
  end; 
  If (Pos(Str1,S)<>0) and (Str2='') then 
  begin 
    Result:=Trim(Copy(S,Pos(Str1,S)+Length(Str1),MaxInt)); 
    Exit; 
  end; 
  temp:=Trim(Copy(S,Pos(Str1,S)+Length(Str1),MaxInt)); 
  Result:=Trim(Copy(temp,0,Pos(Str2,temp)-1)); 
end; 
 
 
procedure ListView_AddColumus(ListView:TListView;ColumnsList:TStringList);  //添加列表头 
var 
  i,ColumnsCount:Integer; 
  ColumnText:string; 
  ColumnWidth:Integer; 
begin 
  ColumnsCount:=ColumnsList.Count; 
  if ColumnsCount=0 then exit; 
  for i:=0 to ColumnsCount-1 do 
  begin 
      ColumnText:=GetStr(ColumnsList[i],'[',','); 
      if  ColumnText='' then Continue; 
      ColumnWidth:=StrtoIntDef(GetStr(ColumnsList[i],',',']'),80); 
      with ListView.Columns.Add  do 
      begin 
         Width:=  ColumnWidth; 
         Caption:= ColumnText; 
         if i= ColumnsCount-1 then 
               AutoSize:=True; 
      end; 
  end; 
end; 
 
procedure ListView_AddItems(ListView:TListView;Params:array of string); 
var 
  i:Integer; 
  ListItem:TListItem; 
begin 
     with ListView do 
     begin 
         ListItem:=Items.Add; 
         ListItem.Caption:='0'; 
         for i:=Low(Params) to High(Params) do 
            ListItem.SubItems.Add(Params[i]);  
     end; 
end; 
 
 
 
 
Function ListView_ModifyListViewByCaption(ListView:TListView;ListIndex:Integer; 
      ColumnsText:string;AddStr:string):string; 
var 
  i,ColumnsNum:Integer; 
  ListItem:TListItem; 
begin 
      Result:=''; 
      Trim(ColumnsText); 
      ListItem:= ListView.Items.Item[ListIndex]; 
      ColumnsNum:=ListView.Columns.Count; 
      For i:=1 to ColumnsNum-1 do 
          if  ListView.Columns.Items[i].Caption =ColumnsText then 
          begin 
              if  AddStr='返回' then 
                  Result:= Trim(ListItem.SubItems.Strings[i-1]) 
              else if  AddStr='删除' then 
                  ListItem.Delete 
              else 
                  ListItem.SubItems.Strings[i-1]:= AddStr; 
              Break; 
          end; 
end; 
 
 
procedure ListView_MondifyListSequenceNumber(ListView:TListView);  //修正列表序号 
var 
  i,ListCount:Integer; 
begin 
    ListCount:=ListView.Items.Count; 
    if ListCount=0 then exit; 
    For i:=0 to  ListCount-1 do 
       ListView_ModifyListViewByCaption(ListView,i,'序号',InttoStr(i+1)); 
end; 
 
end.