delphi TListView屬性及用法


//從數據庫表里讀取數據寫入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 排序

 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;

 

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;
 


要想隨時更改ListView 中某一行的字體顏色,要在ListView的 OnCustomDrawItem 的事件中書寫相關的代碼。例如 我想更改選中的某行字體的顏色,則需要在事件中寫入下的代碼:

if item.Index = strtoint(edit1.Text) then //該條件是用於判斷是否符合更改字體顏色的行的條件。
   Sender.Canvas.Font.Color := clred;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM