TListView使用方法1(转)

摘要:
ListView1.Items是分配给ListView1.Item的标准Tlistitems类ListView1.Items(1)。AdddobeginOption:=caption//添加第一个子项。add(aaaaa)//添加以下数据子项。添加(1234);子项。添加(1234);结束;//确保使用WITH结构。(2) 查看价值列表

 

ListView1.Items 为标准 Tlistitems类
ListView1.Items
(1)赋值
with ListView1.Items.Add do
begin
Caption:=caption; //加入第一项
SubItems.add(aaaaa); //加入后面数据
SubItems.add(1234);
SubItems.add(1234);
end; //一定要使用WITH结构.
(2)取值
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)删除
listview1.Items.Item[i].Delete; 删除一条数据
(4)数据类型(长度)定义
i:=ListView1.Items.Count 数据条数
listview1.Items.Item[i].Selected 该条数据选中否(MultiSelect决定单选复选)
(5)其它
ListView1.Items.Clear; 清除数据

Delphi ListView的使用方法(经常使用技巧)

//添加
             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:='第一列内容';
              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;

免责声明:文章转载自《TListView使用方法1(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android精品开源项目整理RenderMonkey 练习 第三天 【OpenGL renderToTexture】下篇

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

随便看看

fullcalendar日历控件知识点集合

除非对于极少的特殊需求,fullcalendar向我们提供的接口不足以满足,才会去改动fullcalendar本身的js文件。这些会议安排一般是保存在server的,在每次页面载入时,fullcalendar得到会议安排的集合,然后依照当中的日期去把事件描绘到日历相应的地方。...

JQuery 导入导出 Excel

正在做一个小项目,从数据库中查询数据放在HTMLTable中.现在想要从这个table中导出数据来.另外用户需要选择导出的列.使用jQuery的导出插件可以完成这个需求.jQueryPlugintoExportHTMLTables例子:导入插件:[javascript]viewplaincopy˂/scr...

【解决方法】ModuleNotFoundError: No module named 'flask._compat'

起源最近发下很多人在我的python课程下面提问,关于安装完扩展运行程序的时候出现如下的莫名其妙的错误Traceback(mostrecentcalllast):File"manage_web.py",line2,infromapplicationimportapp,managerFile"/data/www/private_deploy/python3_y...

常用的Maven 插件

Maven本质上是一个插件框架。其核心不执行任何特定的施工任务。所有这些任务都委托给插件。多年来,Maven社区积累了大量经验,随后形成了成熟的插件生态系统。这个插件在一些Ant到Maven的迁移项目中特别有用。只要在父POM中配置规则,然后由每个人继承,Maven就会在规则被破坏时报告错误。...

记一次Arcgis Server10.2许可过期导致发布图层失败

1.今天,当使用arcmap将地图服务发布到arcgisserver时,发布突然失败。在arcgisserver的管理页面的日志选项中发现错误:未能初始化服务器对象“System/PublicingTools”:0x80004005:错误:(-8003)YourArcGISServerlicense已过期。2.然后在服务器路径中查找文件:...

HTML中canvas的大小调整

今天用到canvas元素,发现它的大小不是像普通dom元素一样,直接设置css样式可以改变的,它会由自己原本的大小伸缩。例如,1.如果直接在canvas元素中添加style='300px;height:300px',这会导致伸缩,如图2.换种方式在css样式表中加入样式设置宽高产生的效果也是一样的。...