Delphi ListView Basic Usage Daquan
The source of this article: Http://hi.baidu.com/python120/item/4ea85f61af94e55b6895e6ac
Add Item or column (field)
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; Note: The code here can also be done directly in the visual editor,
It can also be written as follows
Begin
With ListView1 do
Begin
Columns.Add;
Columns.Add;
Columns.Add;
Viewstyle:=vsreport;
Gridlines:=true;
columns.items[0].caption:= ' process name ';
columns.items[1].caption:= ' process ID ';
columns.items[2].caption:= ' process file path ';
Columns.items[0]. width:=100;
COLUMNS.ITEMS[1]. width:=100;
COLUMNS.ITEMS[2]. width:=150;
End
End
Add record
With ListView1.Items.Add do
Begin
caption:= ' 1212 ';
SubItems.Add (' HH1 ');
SubItems.Add (' HH2 ');
End
Delete
Listview1.items.delete (0);
Reading data from the database table to the ListView
Var
Titem:tlistitem; It is important to predefine temporary record store variables here.
Begin
ListView1.Items.Clear;
With Adoquery1 do
Begin
Close
Sql. Clear;
Sql. Add (' Select Spmc,jg,sl from Kcxs ');
Open;
ListView1.Items.Clear;
While don't 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
Delete
listview1.deleteselected;
How to get the value of a column in a ListView selected row
Procedure Tform1.button2click (Sender:tobject);
Begin
ShowMessage (Listview1.selected.subitems.strings[1]); Returns the value in the third column of the selected row
End
ShowMessage (listView1.Selected.Caption); Returns the value of the first column of the selected row.
Value of 1th column:-->>> ListView1.Selected.Caption
Value of column I (i>1):-->>> Listview1.selected.subitems.strings[i]
LISTVIEW1.ITEMS.ITEM[1]. Subitems.gettext); Get the value of a column in a ListView row
Edit2.text: = ListView1. Items[i]. Subitems.strings[0]; Read the 2nd column of line I
Returns all child column values for the selected row. Is separated by a carriage return, you also want to peel out the value of the child column you want.
ShowMessage (ListView1.Selected.SubItems.GetText);
Implementation of the ListView simple sort
ListView sort
How to implement click in ascending order, and then click on Descending.
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
===============================================================
Increase
I: = ListView1.Items.Count;
With ListView1 do
Begin
Listitem:=items.add;
listitem.caption:= IntToStr (i);
ListItem.SubItems.Add (' +inttostr (i) + ' line ');
ListItem.SubItems.Add (' third column content ');
End
Delete by title
For i:=listview1.items.count-1 Downto 0 do
If Listview1.items[i]. Caption = Edit1.text Then
Begin
Listview1.items.item[i]. Delete (); Delete the currently selected row
End
Select a row
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); Select the first article
Begin
ListView1. SetFocus;
ListView1. Items[0]. Selected: = True;
End
Procedure Tform1.button1click (Sender:tobject); Select the Last Bar
Begin
ListView1. SetFocus;
ListView1. ITEMS[LISTVIEW1.ITEMS.COUNT-1]. Selected: = True;
End
It's a universal process.
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
This is the call procedure, you can arbitrarily specify the item to move, the following is the current (Selected) item
Listviewitemmoveupdown (ListView1, listview1.selected, True, true);//Move Up
Listviewitemmoveupdown (ListView1, listview1.selected, False, True);//Move Down
How to use TListView components
Referencing the Commctrl unit
Procedure Tform1.button1click (Sender:tobject);
Begin
Listview_deletecolumn (Mylistview.handle, i);//i is the ordinal of the column to be deleted, starting with 0
End
Display the information in the table with the ListView:
Procedure ViewChange (Listv:tlistview;table:tcustomadodataset;var I:integer);
Begin
TListView (LISTV). Items.beginupdate; {Listv:listview name}
Try
TListView (LISTV). Items.clear;
With table do {table or query name}
Begin
Active:=true;
First
While don't 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
How to use the Delphi ListView
Some key points in using the ListView. The following is an example of a two-column ListView.
→ Add a line:
With ListView1 do
Begin
Listitem:=items.add;
listitem.caption:= ' first column of content ';
LISTITEM.SUBITEMS.ADD (' second column content ');
End
→ Empty ListView1:
ListView1.Items.Clear;
→ Get the line number of the row that is currently selected and delete the current line:
For i:=0 to Listview1.items.count-1 do
If Listview1.items[i]. Selected then//i=listview1.selected.index
Begin
ListView1.Items.Delete (i); Delete the currently selected row
End
Of course, the ListView has a Onselectitem event, which can be used to determine which row is selected and assign it with a global variable.
→ Read The operation of a column in a row:
Edit1.text: = ListView1. Items[i]. Caption; Read the 1th column of line I
Edit2.text: = ListView1. Items[i]. Subitems.strings[0]; Read the 2nd column of line I
Edit3.text: = ListView1. Items[i]. SUBITEMS.STRINGS[1]; Read the 3rd column of line I
By analogy, you can read the whole column in a loop.
→ Move the focus up one line:
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
However, in Delphi6, the ListView has one more ItemIndex attribute, so as long as
Listview1.setfocus;
listview1.itemindex:=3;
You can set the focus.
Can Delphi's ListView achieve alternating colors?
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
To change the font color of a row in a ListView at any time, write the relevant code in the Oncustomdrawitem event of the ListView. For example, if I want to change the color of a selected row font, you need to write the following code in the event:
If item. Index = Strtoint (edit1. Text) then//The condition is the condition used to determine whether a line that changes the font color is met.
Sender.Canvas.Font.Color: = clred;
Delphi ListView Basic Usage Daquan [Delphi]