ListView的用法 一

2021-04-12 15:55:45 字數 4465 閱讀 4455

listview的用法(一)

//增加

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;

try

tlistview(listv).items.clear;

with table do

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;

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:= e0f0d7

else sender.canvas.brush.color:= f0eed7;

sender.canvas.fillrect(item.displayrect(dricon));

end;

listview的一些用法

listview使用中的一些要點。以下以乙個兩列的listview為例。增加一行 with listview1 do begin listitem items.add listitem.caption 第一列內容 listitem.subitems.add 第二列內容 end 清空listview1...

listview的一些用法

listview使用中的一些要點。以下以乙個兩列的listview為例。增加一行 with listview1 do begin listitem items.add listitem.caption 第一列內容 listitem.subitems.add 第二列內容 end 清空listview1...

ListView巢狀ListView的思路

1.在activity中,布局中新增原生listview作為 父listview 2.在父listview中新增自定義的 子listview 3.子listview中的布局為單個item的布局。4.書寫父listview的adapter public class eventcitylistadapt...