1、Delphi ListView 基本用法大全/增加项或列( 字段)ListView1.Clear;ListView1.Columns.Clear;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Items0.Caption:=id;ListView1.Columns.Items1.Caption:=type;ListView1.Columns.Items2.Caption:=title;ListView1.Columns.Items2.Width:=300;Listview1
2、.ViewStyle:=vsreport;Listview1.GridLines:=true; /注:此处代码也可以直接在可视化编辑器中完成,也可写成以下这样beginwith listview1 dobeginColumns.Add;Columns.Add;Columns.Add;ViewStyle:=vsreport;GridLines:=true;columns.items0.caption:=进程名;columns.items1.caption:=进程 ID;columns.items2.caption:=进程文件路径;Columns.Items0.Width:=100;Columns
3、.Items1.Width:=100;Columns.Items2.Width:=150;endend;/增加记录with listview1.items.add do begin caption:=1212; subitems.add(hh1); subitems.add(hh2); end;/删除 listview1.items.delete(0);/从数据库表里读取数据写入 ListviewvarTitem:Tlistitem; /此处一定要预定义临时记录存储变量.beginListView1.Items.Clear;with adoquery1 dobeginclose;sql.Cle
4、ar;sql.Add(select spmc,jg,sl from kcxs);Open;ListView1.Items.Clear;while not eof dobeginTitem:=ListView1.Items.add;Titem.Caption:=FieldByName(spmc).Value;Titem.SubItems.Add(FieldByName(sl).Value);Titem.SubItems.Add(FieldByName(jg).Value);next;end;/删除 ListView1.DeleteSelected;/如何取得 ListView 中选中行的某一列的
5、值procedure TForm1.Button2Click(Sender: TObject);beginShowMessage(ListView1.Selected.SubItems.Strings1); /返回选中行第三列中的值end;showMessage(listView1.Selected.Caption); /返回选中行第一列的值.第 1 列的值: ListView1.Selected.Caption 第 i 列的值 (i1): ListView1.Selected.SubItems.StringsiListView1.Items.Item1.SubItems.GetText);
6、/取得 listview 某行某列的值Edit2.Text := listview1.Itemsi.SubItems.strings0; /读第 i 行第 2 列返回选中行所有子列值.是以回车符分开的,你还要从中剥离出来你要的子列的值。showMessage(ListView1.Selected.SubItems.GetText); ListView 简单排序的实现ListView 排序怎样实现单击一下按升序,再单击一下按降序。function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; std
7、call;beginif ColumnIndex = 0 thenResult := CompareText(Item1.Caption,Item2.Caption)elseResult := CompareText(Item1.SubItemsColumnIndex-1,Item2.SubItemsColumnIndex-1)end;procedure TFrmSrvrMain.ListView1ColumnClick(Sender: TObject;Column: TListColumn);beginListView1.CustomSort(CustomSortProc,Column.In
8、dex);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.Itemsi.Caption = Edit1.Text then begin Lis
9、tView1.Items.Itemi.Delete(); /删除当前选中行 end;/选中一行 if ListView1.Selected 0) then begin ListView1.SetFocus; ListView1.Items.Itemi-1.Selected := True; end; 不过在 Delphi6 中,ListView 多了一个 ItemIndex 属性,所以只要 ListView1.SetFocus; ListView1.ItemIndex:=3; 就能设定焦点了。Delphi 的 listview 能实现交替颜色么? procedure TForm1.ListVi
10、ew1CustomDrawItem( 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;