delphi TListView属性及用法

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

 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:='第一列内容';
ListItem.SubItems.Add('第二列内容');
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;
 


要想随时更改ListView 中某一行的字体颜色,要在ListView的 OnCustomDrawItem 的事件中书写相关的代码。例如 我想更改选中的某行字体的颜色,则需要在事件中写入下的代码:

if item.Index = strtoint(edit1.Text) then //该条件是用于判断是否符合更改字体颜色的行的条件。
   Sender.Canvas.Font.Color := clred;

转载于:https://www.cnblogs.com/mingdep/archive/2012/01/05/2313022.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值