找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 14917|回复: 17

Delphi ListView用法太全应该有的技巧全部在这里可以总结

[复制链接]
发表于 2017-7-31 19:28:35 | 显示全部楼层 |阅读模式
本帖最后由 脚本工具 于 2017-7-31 19:31 编辑

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

ListView1.Items.Item[1].SubItems.GetText); //取得listview某行某列的值

Edit2.Text := listview1.Items.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.Caption = Edit1.Text then
begin
ListView1.Items.Item.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.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.Selected then //i=ListView1.Selected.index
begin
ListView1.Items.Delete(i); //删除当前选中行
end;
当然,ListView有OnSelectItem事件,可以判断选择了哪行,用个全局变量把它赋值出来。
→读某行某列的操作:
Edit1.Text := listview1.Items.Caption; //读第i行第1列
Edit2.Text := listview1.Items.SubItems.strings[0]; //读第i行第2列
Edit3.Text := listview1.Items.SubItems.strings[1]; //读第i行第3列
以次类推,可以用循环读出整列。
→将焦点上移一行:
For i:=0 to ListView1.Items.Count-1 Do
If (ListView1.Items.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 XE6关于ListView使用的一些基本方法,目前XE6的ListView在用户体验方便是越来越好,将ListView放到Form上面,通过置ItemAppearance属性,就能够设置出很好的效果


[附件已丢失]



ItemAppearance的值不同,Item的属性值会随之变化,主要是以下几项:
  • Item.Text:Item左侧显示的标题
  • Item.Bitmap:左侧显示的图标
  • Item.ButtonText:右测按钮显示的标题
  • Item.Detail:右侧显示的详细内容

在为ListVIew添加数据,主要有两个方法1:编写代码,2:binding方法来实现可视化操作。

1:首先演示下通过代码来添加ListView数据,其实在Delphi XE6的安装目录的Demo下面,有很多这样的例子,大家可以去学习下
  • var
  •   LItem: TListViewItem;
  •   I: Integer;
  • begin
  •   ListView1.BeginUpdate;//提高效率
  •   try
  •     for I := 1 to 10 do
  •     begin
  •       LItem := ListView1.Items.Add;
  •       LItem.Text := IntToStr(I);
  •     end;
  •   finally
  •     ListView1.EndUpdate;
  •   end;
  • end;

[color=rgb(51, 102, 153) !important]复制代码


2:是利用binding,完全可视化的方式。放置一个TProtoTypeBindSource,为其增加几个Field,然后利用右键bind Visually,即binding可视设计器,将Field联接到item的Text等属性。如图:

[附件已丢失]



ProtoTypeBindSource,为我们准备好了测试数据,这样即时得到了运行期才能看到的结果。

本文章介绍了Delphi XE6关于ListView使用的一些基本方法,目前XE6的ListView在用户体验方便是越来越好,将ListView放到Form上面,通过置ItemAppearance属性,就能够设置出很好的效果


[附件已丢失]



ItemAppearance的值不同,Item的属性值会随之变化,主要是以下几项:
  • Item.Text:Item左侧显示的标题
  • Item.Bitmap:左侧显示的图标
  • Item.ButtonText:右测按钮显示的标题
  • Item.Detail:右侧显示的详细内容

在为ListVIew添加数据,主要有两个方法1:编写代码,2:binding方法来实现可视化操作。

1:首先演示下通过代码来添加ListView数据,其实在Delphi XE6的安装目录的Demo下面,有很多这样的例子,大家可以去学习下
  • var
  •   LItem: TListViewItem;
  •   I: Integer;
  • begin
  •   ListView1.BeginUpdate;//提高效率
  •   try
  •     for I := 1 to 10 do
  •     begin
  •       LItem := ListView1.Items.Add;
  •       LItem.Text := IntToStr(I);
  •     end;
  •   finally
  •     ListView1.EndUpdate;
  •   end;
  • end;

[color=rgb(51, 102, 153) !important]复制代码


2:是利用binding,完全可视化的方式。放置一个TProtoTypeBindSource,为其增加几个Field,然后利用右键bind Visually,即binding可视设计器,将Field联接到item的Text等属性。如图:

[附件已丢失]



ProtoTypeBindSource,为我们准备好了测试数据,这样即时得到了运行期才能看到的结果。
qq.jpg
102407gsmvsdkvjpvrdxwx.jpg
102613r61b404n0n88dn0v.jpg
102407gsmvsdkvjpvrdxwx.jpg
102613r61b404n0n88dn0v.jpg

0

主题

609

回帖

669

积分

软件网站元老会员

积分
669
发表于 2017-7-31 19:33:51 | 显示全部楼层
服务一流!效果明显,不错,给爱易设计师点个赞!

0

主题

530

回帖

677

积分

软件网站元老会员

积分
677
发表于 2017-8-1 04:40:49 | 显示全部楼层
的挺好的,操作起来简单,老板也很有耐心,满意

0

主题

588

回帖

686

积分

软件网站元老会员

积分
686
发表于 2017-8-1 05:23:37 | 显示全部楼层
不知该说些什么。。。。。。就是谢谢

0

主题

557

回帖

622

积分

软件网站元老会员

积分
622
发表于 2017-8-3 13:56:16 | 显示全部楼层
超快,包装也很好,服务态度非常好,喜欢

0

主题

544

回帖

602

积分

软件网站元老会员

积分
602
发表于 2017-8-4 03:07:26 | 显示全部楼层
快,页面设计很满意,而且也很,老板人不错,喜欢

0

主题

567

回帖

630

积分

软件网站元老会员

积分
630
发表于 2017-8-4 21:17:31 | 显示全部楼层
特别满意,很细心很周到的为我司考虑设计方案,耐心的一点点修改。我之前的网站就是077号做的,这次添加搜索栏也是找的她以后还要找她~

0

主题

530

回帖

677

积分

软件网站元老会员

积分
677
发表于 2017-8-4 23:41:40 | 显示全部楼层
哈哈

0

主题

567

回帖

630

积分

软件网站元老会员

积分
630
发表于 2017-8-5 00:13:11 | 显示全部楼层
很好,服务很好,耐心细致,下次还会来的

0

主题

578

回帖

624

积分

软件网站元老会员

积分
624
发表于 2017-8-7 23:35:20 | 显示全部楼层
及时做出评价,系统默认好评!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|

GMT+8, 2026-10-22 13:18

安卓脚本软件 · 键鼠盒子 · HID · 机械臂

快速回复 返回顶部 返回列表

随机推荐 · 热帖 / 新帖 / 随便看看

社区会员(随机展示)