Delphi ListView基本用法大全

摘要:
columns.items[2]标题;Columns.Items[2].宽度;hh1&Subitems.add(&jg;ListView1。项目。清除=FieldByName(&spmc&sl&Title.SubItems.Add(FieldByName,&//删除ListView1。删除选定项;

//增加项或列(字段)

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;               //注:此处代码也可以直接在可视化编辑器中完成,

也可写成以下这样

begin
with listview1 do
begin
Columns.Add;
Columns.Add;
Columns.Add;
ViewStyle:=vsreport;
GridLines:=true;
columns.items[0].caption:='进程名';
columns.items[1].caption:='进程ID';
columns.items[2].caption:='进程文件路径';
Columns.Items[0].Width:=100;
Columns.Items[1].Width:=100;
Columns.Items[2].Width:=150;
end
end;

//增加记录
with listview1.items.add do
begin
caption:='1212';
subitems.add('hh1');
subitems.add('hh2');
end;

//删除
listview1.items.delete(0);

//从数据库表里读取数据写入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 排序


怎样实现单击一下按升序,再单击一下按降序。
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;

===============================================================

//增加
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; {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:=&apos;第一列内容&apos;;
ListItem.SubItems.Add(&apos;第二列内容&apos;);
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;

免责声明:文章转载自《Delphi ListView基本用法大全》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jQuery(二)MATLAB相关快捷键以及常用函数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

quartz结合多线程处理后台业务

  最近项目中有播放视频的需求,技术选型采用UMS播放器,免费版只能播放FLV格式的视频文件,因此需要对用户上传的视频进行格式转换,转换工具为FormatFactory,功能还是比较强大的。但是面临的一个问题,视频转换是非常耗时的,上传完直接转换是没法接受的,于是决定采用quartz,以任务调度的方式,在后台进行转换,具体步骤如下:   1.定义一个任务...

SpringCloud(10)使用Spring Cloud OAuth2和JWT保护微服务

【本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究。若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!】 采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务。采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务...

wpf 窗体翻页效果

点击设置翻页,取消翻回来 1.xaml 1 <Window x:Class="_3D翻页动画.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.m...

shell 参数意义和重定向

$1,$2,... 特定第几个参数, $0 表示当前执行的进程名,script 本身的名字,或者在正则表达式中表示整行输出   $# 命令行或者是位置参数的个数   $* 所有的位置参数,被作为一个单词.   $@ 与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被完整地传递   $- 传递给脚本的falg   $!   在后台运行...

PyMySQL模块

  PyMySQL是Python操作数据库的模块,在Python3.x版本中用于连接MySQL服务器,即作为Python操作MySQL数据库对象的接口。 安装PyMySQL   pip3 install PyMySQL 导入PyMySQL   import pymysql 连接数据库   格式:conn = pymysql.connect(host=' '...

通过chkrootkit学习如何在linux下检测RootKit

Rootkit是一种特殊的恶意软件,它的功能是在安装目标上隐藏自身及指定的文件、进程和网络链接等信息,比较多见到的是Rootkit一般都和木马、后门等其他恶意程序结合使用。Rootkit一词更多地是指被作为驱动程序,加载到操作系统内核中的恶意软件。 chkrootkit简介 chkrootkit是一个linux下检RootKit的脚本,在某些检测中会调用当...