ListView的使用技巧

来源:互联网 发布:金蝶k3数据字典 下载 编辑:程序博客网 时间:2024/04/28 23:48

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3528650120430763&amp;dt=1183890684015&amp;lmt=1183890684&amp;format=468x60_as&amp;output=html&amp;correlator=1183890684000&amp;url=http%3A%2F%2Fgmai9999.googlepages.com%2Fhome&amp;ad_type=text_image&amp;ui=rc%3A0&amp;cc=100&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe> 
//全部选择

  for I:=0 to ListView1.Items.Count-1 do
  begin
     ListView1.Items.Item[i].Checked := true;
  end;

//增加
  i := ListView1.Items.Count;
  with ListView1 do
  begin
    ListItem:=Items.Add;
    ListItem.Caption:= IntToStr(i);
    ListItem.SubItems.Add('第 '+IntToStr(i)+' 行');
    ListItem.SubItems.Add('第三列内容');
  end;

//按标题删除
  for i:=ListView1.Items.Count-1 downto 0 Do
    if ListView1.Items[i].Caption = Edit1.Text then
    begin
      ListView1.Items.Item[i].Delete();  //删除当前选中行
    end;

//选中一行
  if ListView1.Selected <> nil then
  Edit1.Text := ListView1.Selected.Caption;


//   listview1.Items[Listview1.Items.Count -1].Selected := True;
//   listview1.Items[Listview1.Items.Count -1].MakeVisible(True);  
procedure TForm1.Button2Click(Sender: TObject); // 选择第一条
begin
  listview1.SetFocus;
  listview1.Items[0].Selected := True;
end;

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-0494631679463850&amp;dt=1183254053140&amp;lmt=1183254053&amp;prev_fmts=120x600_as&amp;format=468x60_as&amp;output=html&amp;correlator=1183254053031&amp;url=http%3A%2F%2Fpages.google.com%2Fdraft%2FBlueStar.Skyking%2Fadsensepage%3Fauthtoken%3D91b825ffd39d84145aea501a0792e0ea1658201d&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fpages.google.com%2Fpreview%2FBlueStar.Skyking%2Fadsensepage%3Fauthtoken%3D06516fbfa328a5527eb935f1e169cb23d43c0351%26no-cache%3D7350381924487911&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe> 
procedure TForm1.Button1Click(Sender: TObject);  // 选择最后一条
begin
  listview1.SetFocus;
  listview1.Items[Listview1.Items.Count -1].Selected := True;
end;  

//这是个通用的过程
procedure ListViewItemMoveUpDown(lv : TListView; Item : TListItem; MoveUp, SetFocus : Boolean);
var
  DestItem : TListItem;
begin
  if (Item = nil) or
     ((Item.Index - 1 < 0) and MoveUp) or
     ((Item.Index + 1 >= lv.Items.Count) and (not MoveUp))
    then Exit;
  lv.Items.BeginUpdate;
  try
    if MoveUp then
      DestItem := lv.Items.Insert(Item.Index - 1)
    else
      DestItem := lv.Items.Insert(Item.Index + 2);
    DestItem.Assign(Item);
    lv.Selected := DestItem;
    Item.Free;
  finally
    lv.Items.EndUpdate;
  end;
  if SetFocus then lv.SetFocus;
  DestItem.MakeVisible(False);
end;

//此为调用过程,可以任意指定要移动的Item,下面是当前(Selected)Item
  ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移
  ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移 


TListView组件使用方法

引用CommCtrl单元

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,从0开始

end;

用LISTVIEW显示表中的信息:
procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer);
  begin
    tlistview(listv).Items.BeginUpdate;   {listv:listview名}
    try
      tlistview(listv).Items.Clear;
      with table do         {table or query名}
      begin
        active:=true;
        first;
        while not eof do
        begin
          listitem:=tlistview(listv).Items.add;
          listitem.Caption:=trim(table.fields[i].asstring);
//          listitem.ImageIndex:=8;
          next;
        end;
      end;
    finally
      tlistview(listv).Items.EndUpdate;
    end;
 end;
 
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-0494631679463850&amp;dt=1183254053140&amp;lmt=1183254053&amp;prev_fmts=120x600_as&amp;format=468x60_as&amp;output=html&amp;correlator=1183254053031&amp;url=http%3A%2F%2Fpages.google.com%2Fdraft%2FBlueStar.Skyking%2Fadsensepage%3Fauthtoken%3D91b825ffd39d84145aea501a0792e0ea1658201d&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fpages.google.com%2Fpreview%2FBlueStar.Skyking%2Fadsensepage%3Fauthtoken%3D06516fbfa328a5527eb935f1e169cb23d43c0351%26no-cache%3D7350381924487911&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe> 

ListView使用中的一些要点。以下以一个两列的ListView为例。
  →增加一行:
with ListView1 do
  begin
    ListItem:=Items.Add;
    ListItem.Caption:='第一列内容';
    ListItem.SubItems.Add('第二列内容');
  end;
  →清空ListView1:
ListView1.Items.Clear;
  →得到当前被选中行的行的行号以及删除当前行:
For i:=0 to ListView1.Items.Count-1 Do
  If ListView1.Items[i].Selected then  //i=ListView1.Selected.index
    begin
      ListView1.Items.Delete(i);  //删除当前选中行
    end;
当然,ListView有OnSelectItem事件,可以判断选择了哪行,用个全局变量把它赋值出来。
  →读某行某列的操作:
Edit1.Text := listview1.Items[i].Caption;  //读第i行第1列
Edit2.Text := listview1.Items[i].SubItems.strings[0];  //读第i行第2列
Edit3.Text := listview1.Items[i].SubItems.strings[1];  //读第i行第3列
以次类推,可以用循环读出整列。
  →将焦点上移一行:
For i:=0 to ListView1.Items.Count-1 Do
  If (ListView1.Items[i].Selected) and (i>0) then
    begin
      ListView1.SetFocus;
      ListView1.Items.Item[i-1].Selected := True;
    end;
不过在Delphi6中,ListView多了一个ItemIndex属性,所以只要
ListView1.SetFocus;
ListView1.ItemIndex:=3;
就能设定焦点了。


 Delphi的listview能实现交替颜色么?
procedure TForm1.ListView1CustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  i: integer;
begin
  i:= (Sender as TListView).Items.IndexOf(Item);
  if odd(i) then sender.Canvas.Brush.Color:= $02E0F0D7
  else sender.Canvas.Brush.Color:= $02F0EED7;
  Sender.Canvas.FillRect(Item.DisplayRect(drIcon));
end;   
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3528650120430763&amp;dt=1183890684015&amp;lmt=1183890684&amp;format=468x60_as&amp;output=html&amp;correlator=1183890684000&amp;url=http%3A%2F%2Fgmai9999.googlepages.com%2Fhome&amp;ad_type=text_image&amp;ui=rc%3A0&amp;cc=100&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe> 

ListView 导出到excel文件

uses   ComObj;  
  procedure   Tzong_Form.BitBtn2Click(Sender:   TObject);  
  var  
      myexcel:variant;  
      workbook:olevariant;  
      worksheet:olevariant;  
      k:integer;  
  begin  
          try  
   
        myexcel:=createoleobject('excel.application');  
        myexcel.application.workbooks.add;  
        myexcel.caption:='将数据导入到EXCEL表中';  
        myexcel.application.visible:=true;  
        workbook:=myexcel.application.workbooks[1];  
        worksheet:=workbook.worksheets.item[1];  
        except  
          showmessage('EXCEL不存在!');  
        end;  
        worksheet.cells[1,1]:='年月';  
        worksheet.cells[1,2]:='余额';  
        worksheet.cells[1,3]:='收入';  
        worksheet.cells[1,4]:='支出';  
      for   k:=0   to   listview1.items.count-1   do  
          begin  
              worksheet.cells[k+2,1]:=Listview1.items[k].caption;//把第一列的数据放入Excel的第一列  
      //其他列的同理  
              worksheet.cells[k+2,2]:=Listview1.items[k].SubItems[0];  
              worksheet.cells[k+2,3]:=Listview1.items[k].SubItems[1];  
              worksheet.cells[k+2,4]:=Listview1.items[k].SubItems[2];  
   
        end;  
  end;
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3528650120430763&amp;dt=1183890684015&amp;lmt=1183890684&amp;format=468x60_as&amp;output=html&amp;correlator=1183890684000&amp;url=http%3A%2F%2Fgmai9999.googlepages.com%2Fhome&amp;ad_type=text_image&amp;ui=rc%3A0&amp;cc=100&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe>

原创粉丝点击