Delphi ListView基本用法大全
Delphi ListView基本用法大全
//增加项或列(字段)
ListView1.Clear;
ListView1.Columns.Clear;
ListView1.Columns.Add;
ListView1.Columns.Add;
ListView1.Columns.Add;
ListView1.Columns.Items[0].Caption:='id';
ListView1.Columns.Items[1].Caption:='type';
ListView1.Columns.Items[2].Caption:='title';
ListView1.Columns.Items[2].Width:=300;
Listview1.ViewStyle:=vsreport;
Listview1.GridLines:=true; //注:此处代码也可以直接在可视化编辑器中完成,
也可写成以下这样
begin
with listview1 do
begin
Columns.Add;
Columns.Add;
Columns.Add;
ViewStyle:=vsreport;
GridLines:=true;
columns.items[0].caption:='进程名';
columns.items[1].caption:='进程ID';
columns.items[2].caption:='进程文件路径';
Columns.Items[0].Width:=100;
Columns.Items[1].Width:=100;
Columns.Items[2].Width:=150;
end
end;
//增加记录
with listview1.items.add do
begin
caption:='1212';
subitems.add('hh1');
subitems.add('hh2');
end;
//删除
listview1.items.delete(0);
//从数据库表里读取数据写入Listview
var
Titem:Tlistitem; //此处一定要预定义临时记录存储变量.
begin
ListView1.Items.Clear;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select spmc,jg,sl from kcxs');
Open;
ListView1.Items.Clear;
while not eof do
begin
Titem:=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中选中行的某一列的值
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(ListView1.Selected.SubItems.Strings[1]); //返回选中行第三列中的值
end;
showMessage(listView1.Selected.Caption); //返回选中行第一列的值.
第1列的值: -->>> ListView1.Selected.Caption
第i列的值(i>1):-->>> ListView1.Selected.SubItems.Strings[i]
ListView1.Items.Item[1].SubItems.GetText); //取得listview某行某列的值
Edit2.Text := listview1.Items[i].SubItems.strings[0]; //读第i行第2列
返回选中行所有子列值.是以回车符分开的,你还要从中剥离出来你要的子列的值。
showMessage(ListView1.Selected.SubItems.GetText);
ListView 简单排序的实现
ListView 排序
怎样实现单击一下按升序,再单击一下按降序。
function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall;
begin
if ColumnIndex = 0 then
Result := CompareText(Item1.Caption,Item2.Caption)
else
Result := CompareText(Item1.SubItems[ColumnIndex-1],Item2.SubItems[ColumnIndex-1])
end;
procedure TFrmSrvrMain.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc,Column.Index);
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;
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组件使用方