Delphi TListview[2] 常用方法和技巧
(1)赋值
?
|
1 2 3 4 5 6 7 8 9 10 11 12
|
with ListView1.Items.Add do begin Caption:=caption; //添加第一项 SubItems.add(11); // SubItems.add(22); SubItems.add(33);end;with ListView1 do begin ListItem:=Items.Add; ListItem.Caption:='第一列'; ListItem.SubItems.Add('第二列');end; |
(2)取值
?
|
1 2 3 4 5 6 7
|
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列Listview1.Items.Item[i].Caption;// 取得某条数据标题Listview1.Items.Item[i].SubItems.Strings[j];// 取得某条数据内容Listview1.Items.Item[i].SubItems.CommaText;// 一条记录的全部内容,格式: "标题","内容1","内容2",....Listview1.Items.Item[i].SubItems.Text;// 一条记录的全部内容,格式: 标题,内容,内容2.... |
(3)删除
?
|
1
|
Listview1.Items.Item[i].Delete; //删除一条数据 |
(4)数据类型(长度)定义
?
|
1 2
|
i:=ListView1.Items.Count ;//数据条数Listview1.Items.Item[i].Selected; //该条数据选中否(MultiSelect决定单选复选) |
(5) 清除数据
?
|
1 2
|
ListView1.Clear; //清除数据ListView1.Items.Clear; //清除数据 |
(6)插入一列
?
|
1 2
|
DestItem := Listview1.Items.Insert(CurItem.Index );//在当前列前面插入一列DestItem := Listview1.Items.Insert(CurItem.Index + 1);//在当前列后面插入一列 |
(7)删除一列
?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
begin ListView1.Items.Delete(tItem.Index); //删除当前列 ListView1.Items.Delete(tItem.Index + 1); //删除当前列后面一列,要先判断其存在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)ItemListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移 |
示例1:实现不同行颜色不一样
?
|
1 2 3 4 5 6 7
|
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);begin //OwnerDraw应该设置为False if Item.Index mod 2 = 0 then Sender.Canvas.Brush.Color := clSkyBlue else Sender.Canvas.Brush.Color := clWhite;end; |
示例2:实现单击列表标题进行排序
?
|
1 2 3 4 5 6 7 8 9 10 11 12
|
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 TFrmMain.TypeLvColumnClick(Sender: TObject; Column: TListColumn);begin TypeLv.CustomSort(@CustomSortProc, Column.Index);end;: |
示例3:判断当前单击的哪一行
?
|
1 2 3 4 5 6
|
begin GetCursorPos(p); p := lvList.ScreenToClient(p); CurItem := lvList.GetItemAt(p.X, p.Y); ARect:= CurItem.DisplayRect(drBounds);end; |
示例4:根据存入的Data找到指定的Item
?
|
1
|
curItem := lvRoomList.FindData(0, AData, True, False);: |
示例5:在vsReport模式下第一列插入两个图标
SmallImages,StateImages分别关联一个TimageList对象,TimageList对象对象中存入所有要显示的图标,对与TlistView 的vsReport模式下的每一条记录TlistItem对象tItem,
可分别通过 tItem.ImageIndex 和 tItem.StateIndex 来控制显示TimageList对象中的哪个图标,
一般tItem.StateIndex显示的图标在前,tItem.ImageIndex显示的图标在后,
若要判断单击事件是鼠标单击哪个图标的,可通过GetCursorPos(p);获得鼠标坐标后判断横坐标即P.X判断。
赞 (0)
