delphi 7中Tlistview的使用

今天刚注册了,  先发一篇文章试试, 照朋友的话说, 我是稀缺人才, 现在没有人用delphi了, anyway, 把自己多年的一点经验share一下.

如果不使用第3方空间的话, delphi 中的tlistview是个好东西, , 可以作很多事, 我用的最多的是在viewstyle=vsReport的状态下, 下面把握自己做的一些通用的function share一下

1.从一个listview 移动到另一个中, 即选择操作

//如果checked=true,则move item 为checked, 否则默认为selected item
//返回被move的item的caption的组合, 格式为'caption,caption,caption'
function MoveLvItem(lvOrig,lvDest:TListView;checked:boolean=false):string;
var i,j:integer;
    ItemList:TObjectlist;
    listitem,newlistitem:TListItem;
begin
  ItemList:=TObjectList.Create(false);
  //获取所有需要移动的item
  if not checked then
  begin
    for i:=lvOrig.Selected.Index to lvOrig.Items.Count-1 do
    begin
      if lvOrig.Items[i].Selected then ItemList.Add(lvOrig.Items[i]);
    end;
  end
  else
  begin
    for i:=0 to lvOrig.Items.Count-1 do
    begin
      if lvOrig.Items[i].Checked then ItemList.Add(lvOrig.Items[i]);
    end;
  end;
  //在lvdest创建需要move的item的copy
  for i:=0 to ItemList.Count-1 do
  begin
    listitem:=ItemList[i] as TListItem;
    newlistitem:=lvDest.Items.Add;
    newlistitem.Caption:=listitem.Caption;
    for j:=0 to listitem.SubItems.Count-1 do
    begin
      newlistitem.SubItems.Add(listitem.SubItems[j]);
    end;
  end;
  //记录caption list 同时删除
  Result:=(ItemList[0] as TListItem).Caption;
  (ItemList[0] as TListItem).Delete;
  for i:=1 to ItemList.Count-1 do
  begin
    Result:=Result+','+(ItemList[i] as TListItem).Caption;
    (ItemList[i] as TListItem).Delete;
  end;
  ItemList.Free;
end;

2.  对Tlistview 的column进行排序,

下面的function CompareItem是能够被tlistview.customsort直接作为参数调用的, 也可以通过tlistview.oncompare来调用.在这个function后面会分别说一下两种方法, 这个function会自动判断数字(数字中可以带千分位)或string来进行排序,并且根据lParamSort正负决定升序还是降序

function CompareItem(Item1, Item2: TListItem;
  lParamSort: Integer):Integer; stdcall;
var CompItem1,compItem2:string;
    item1Num,item2Num:boolean;
    Date1,date2:TDate;
    F1,F2:real;
    ColIndex,gsort:Integer;
begin
    f1:=0;
    f2:=0;
    gsort:=1;
    if lParamSort<0 then
    begin
      gSort:=-gSort;
      ColIndex:=-lParamSort;
    end
    else
    begin
      ColIndex:=lParamSort;
    end;
    CompareItem:=0;
    item1Num:=true;
    item2Num:=true;
    if ColIndex=1 then
    begin
        CompItem1:=item1.Caption;
        compItem2:=item2.Caption;
    end
    else
    begin
        CompItem1:=item1.SubItems[ColIndex-2];
        compItem2:=item2.SubItems[ColIndex-2];
    end;
    try
        date1:=StrToDate(CompItem1);
        date2:=StrToDate(CompItem2);
        if date1>date2 then CompareItem:=gSort;
        if date1=date2 then CompareItem:=0;
        if date1<date2 then CompareItem:=-gSort;
    except
        on EConvertError do
        begin
          try
              F1:=strtofloat(StringReplace(CompItem1,',','',[rfReplaceAll]));
          except
              item1Num:=false;
          end;
          try
              F2:=strtofloat(StringReplace(CompItem2,',','',[rfReplaceAll]));
          except
              item2Num:=false;
          end;
          if (item1Num and item2Num)  then
          begin
              if f1>f2 then
                CompareItem:=gSort;
              if f1=f2 then
                CompareItem:=0;
              if f1<f2 then
                CompareItem:=-gSort;
          end
          else
          begin
            if item1Num then
                CompareItem:=-1
            else if item2Num then
                CompareItem:=1
            else if gSort=1 then
                CompareItem:=CompareStr(CompItem1,compItem2)
            else
                CompareItem:=CompareStr(CompItem2,compItem1);
          end;
        end;
    end;
end;
调用方法:

1. 直接调用: 注意Origcol 是全局变量, 用来记录上次点击的column, 如果两次点击同一个column, 将在降序升序之间切换, 注意CompareItem前加@ 表示function的地址

var Origcol;
procedure Tform1.listvew1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
    if Abs(Origcol)-1=Column.Index then Origcol:=-Origcol else Origcol:=Column.Index+1;
    (Sender as TListView).CustomSort(@CompareItem,Origcol);
end;

2. 通过tlistview.oncompare 调用, 要想使oncompare有效,  必须在oncolumnclick里调用CustomSort 时sortproc设为nil, 然后在oncompare中加入处理的代码, 可以加入一些其他条件进行排序,如下

procedure Tform1.listvew1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
    if Abs(Origcol)-1=Column.Index then Origcol:=-Origcol else Origcol:=Column.Index+1;
    (Sender as TListView).CustomSort(nil,Origcol);
end;

//oncompare event
procedure Tform1.listview1Compare(Sender: TObject; Item1,
  Item2: TListItem; Data: Integer; var Compare: Integer);
begin
   //先按照checked 状态排序, 再按照column排序, 即 checked listitem永远排在前面
    if item1.Checked and (not Item2.Checked) then Compare:=-1;
    if item2.Checked and (not Item1.Checked) then Compare:=1;
    if item2.Checked and Item1.Checked then Compare:=CompareItem(item1,item2,Data);
    if (not item1.Checked) and (not Item2.Checked) then Compare:=CompareItem(item1,item2,Data);

end;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值