// Add
I: = listview1.items. count;
With listview1 do
Begin
Listitem: = items. Add;
Listitem. Caption: = inttostr (I );
Listitem. subitems. Add ('quarter '+ inttostr (I) + 'row ');
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
Begin
Listview1.setfocus;
Listview1.items [0]. Selected: = true;
End;
Procedure tform1.button1click (Sender: tobject); // select the last
Begin
Listview1.setfocus;
Listview1.items [listview1.items. Count-1]. Selected: = true;
End;
// This is a general 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 process. You can specify any item to be moved. The following is the current (selected) item.
Listviewitemmoveupdown (listview1, listview1.selected, true, true); // move up
Listviewitemmoveupdown (listview1, listview1.selected, false, true); // move down
Tlistview component usage
Reference A commctrl Unit
Procedure tform1.button1click (Sender: tobject );
Begin
Listview_deletecolumn (mylistview. Handle, I); // I is the sequence number of the column to be deleted, starting from 0
End;
Use listview to display information in the table:
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 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;
Some key points in listview usage. The following uses a two-column listview as an example.
→ Add a row:
With listview1 do
Begin
Listitem: = items. Add;
Listitem. Caption: = 'first column content ';
Listitem. subitems. Add ('second column content ');
End;
→ Clear listview1:
Listview1.items. Clear;
→ Get the row number of the selected row and delete the current row:
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, if listview has an onselectitem event, you can determine which row to select and assign a value to it using a global variable.
→ Read a column in a row:
Edit1.text: = listview1.items [I]. Caption; // read Column 1st of row I
Edit2.text: = listview1.items [I]. subitems. Strings [0]; // read Column 2nd of row I
Edit3.text: = listview1.items [I]. subitems. Strings [1]; // read Column 3rd of row I
The entire column can be read cyclically.
→ 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, listview has an itemindex attribute, so as long
Listview1.setfocus;
Listview1.itemindex: = 3;
You can set the focus.
Can the listview of Delphi achieve alternate colors?
Procedure tform1.listview1customdrawitem (
Sender: tcustomlistview; item: tlistitem; State: tcustomdrawstate;
VaR defadradraw: Boolean );
VaR
I: integer;
Begin
I: = (sender as tlistview). Items. indexof (item );
If Odd (I) Then sender. Canvas. Brush. Color: = bytes $02e0f0d7
Else sender. Canvas. Brush. Color: = bytes $02f0eed7;
Sender. Canvas. fillrect (item. displayrect (dricon ));
End;