//增加
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組件使用方法
引用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;