1. //增加项或列(字段)
  2.  
  3. ListView1.Clear;
  4. ListView1.Columns.Clear;
  5. ListView1.Columns.Add;
  6. ListView1.Columns.Add;
  7. ListView1.Columns.Add;
  8. ListView1.Columns.Items[].Caption:='id';
  9. ListView1.Columns.Items[].Caption:='type';
  10. ListView1.Columns.Items[].Caption:='title';
  11. ListView1.Columns.Items[].Width:=;
  12. Listview1.ViewStyle:=vsreport;
  13. Listview1.GridLines:=true; //注:此处代码也可以直接在可视化编辑器中完成,
  14.  
  15. 也可写成以下这样
  16.  
  17. begin
  18. with listview1 do
  19. begin
  20. Columns.Add;
  21. Columns.Add;
  22. Columns.Add;
  23. ViewStyle:=vsreport;
  24. GridLines:=true;
  25. columns.items[].caption:='进程名';
  26. columns.items[].caption:='进程ID';
  27. columns.items[].caption:='进程文件路径';
  28. Columns.Items[].Width:=;
  29. Columns.Items[].Width:=;
  30. Columns.Items[].Width:=;
  31. end
  32. end;
  33.  
  34. //增加记录
  35. with listview1.items.add do
  36. begin
  37. caption:='';
  38. subitems.add('hh1');
  39. subitems.add('hh2');
  40. end;
  41.  
  42. //删除
  43. listview1.items.delete();
  44.  
  45. //从数据库表里读取数据写入Listview
  46.  
  47. var
  48. Titem:Tlistitem; //此处一定要预定义临时记录存储变量.
  49. begin
  50. ListView1.Items.Clear;
  51. with adoquery1 do
  52. begin
  53. close;
  54. sql.Clear;
  55. sql.Add('select spmc,jg,sl from kcxs');
  56. Open;
  57. ListView1.Items.Clear;
  58. while not eof do
  59. begin
  60. Titem:=ListView1.Items.add;
  61. Titem.Caption:=FieldByName('spmc').Value;
  62. Titem.SubItems.Add(FieldByName('sl').Value);
  63. Titem.SubItems.Add(FieldByName('jg').Value);
  64. next;
  65. end;
  66.  
  67. //删除
  68. ListView1.DeleteSelected;
  69.  
  70. //如何取得ListView中选中行的某一列的值
  71.  
  72. procedure TForm1.Button2Click(Sender: TObject);
  73. begin
  74. ShowMessage(ListView1.Selected.SubItems.Strings[]); //返回选中行第三列中的值
  75. end;
  76.  
  77. showMessage(listView1.Selected.Caption); //返回选中行第一列的值.
  78.  
  79. 1列的值: -->>> ListView1.Selected.Caption
  80. i列的值(i>):-->>> ListView1.Selected.SubItems.Strings[i]
  81.  
  82. ListView1.Items.Item[].SubItems.GetText); //取得listview某行某列的值
  83.  
  84. Edit2.Text := listview1.Items[i].SubItems.strings[]; //读第i行第2列
  85.  
  86. 返回选中行所有子列值.是以回车符分开的,你还要从中剥离出来你要的子列的值。
  87.  
  88. showMessage(ListView1.Selected.SubItems.GetText);
  89.  
  90. ListView 简单排序的实现
  91.  
  92. ListView 排序
  93.  
  94. 怎样实现单击一下按升序,再单击一下按降序。
  95. function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall;
  96. begin
  97. if ColumnIndex = then
  98. Result := CompareText(Item1.Caption,Item2.Caption)
  99. else
  100. Result := CompareText(Item1.SubItems[ColumnIndex-],Item2.SubItems[ColumnIndex-])
  101. end;
  102.  
  103. procedure TFrmSrvrMain.ListView1ColumnClick(Sender: TObject;
  104. Column: TListColumn);
  105. begin
  106. ListView1.CustomSort(@CustomSortProc,Column.Index);
  107. end;
  108.  
  109. ===============================================================
  110.  
  111. //增加
  112. i := ListView1.Items.Count;
  113. with ListView1 do
  114. begin
  115. ListItem:=Items.Add;
  116. ListItem.Caption:= IntToStr(i);
  117. ListItem.SubItems.Add('第 '+IntToStr(i)+' 行');
  118. ListItem.SubItems.Add('第三列内容');
  119. end;
  120.  
  121. //按标题删除
  122. for i:=ListView1.Items.Count- downto Do
  123. if ListView1.Items[i].Caption = Edit1.Text then
  124. begin
  125. ListView1.Items.Item[i].Delete(); //删除当前选中行
  126. end;
  127.  
  128. //选中一行
  129. if ListView1.Selected <> nil then
  130. Edit1.Text := ListView1.Selected.Caption;
  131.  
  132. // listview1.Items[Listview1.Items.Count -1].Selected := True;
  133. // listview1.Items[Listview1.Items.Count -1].MakeVisible(True);
  134. procedure TForm1.Button2Click(Sender: TObject); // 选择第一条
  135. begin
  136. listview1.SetFocus;
  137. listview1.Items[].Selected := True;
  138. end;
  139.  
  140. procedure TForm1.Button1Click(Sender: TObject); // 选择最后一条
  141. begin
  142. listview1.SetFocus;
  143. listview1.Items[Listview1.Items.Count -].Selected := True;
  144. end;
  145.  
  146. //这是个通用的过程
  147. procedure ListViewItemMoveUpDown(lv : TListView; Item : TListItem; MoveUp, SetFocus : Boolean);
  148. var
  149. DestItem : TListItem;
  150. begin
  151. if (Item = nil) or
  152. ((Item.Index - < ) and MoveUp) or
  153. ((Item.Index + >= lv.Items.Count) and (not MoveUp))
  154. then Exit;
  155. lv.Items.BeginUpdate;
  156. try
  157. if MoveUp then
  158. DestItem := lv.Items.Insert(Item.Index - )
  159. else
  160. DestItem := lv.Items.Insert(Item.Index + );
  161. DestItem.Assign(Item);
  162. lv.Selected := DestItem;
  163. Item.Free;
  164. finally
  165. lv.Items.EndUpdate;
  166. end;
  167. if SetFocus then lv.SetFocus;
  168. DestItem.MakeVisible(False);
  169. end;
  170.  
  171. //此为调用过程,可以任意指定要移动的Item,下面是当前(Selected)Item
  172. ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移
  173. ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移
  174.  
  175. TListView组件使用方法
  176.  
  177. 引用CommCtrl单元
  178.  
  179. procedure TForm1.Button1Click(Sender: TObject);
  180. begin
  181. ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,从0开始
  182.  
  183. end;
  184.  
  185. LISTVIEW显示表中的信息:
  186. procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer);
  187. begin
  188. tlistview(listv).Items.BeginUpdate; {listv:listview名}
  189. try
  190. tlistview(listv).Items.Clear;
  191. with table do {table or query名}
  192. begin
  193. active:=true;
  194. first;
  195. while not eof do
  196. begin
  197. listitem:=tlistview(listv).Items.add;
  198. listitem.Caption:=trim(table.fields[i].asstring);
  199. // listitem.ImageIndex:=8;
  200. next;
  201. end;
  202. end;
  203. finally
  204. tlistview(listv).Items.EndUpdate;
  205. end;
  206. end;
  207.  
  208. ListView使用中的一些要点。以下以一个两列的ListView为例。
  209. →增加一行:
  210. with ListView1 do
  211. begin
  212. ListItem:=Items.Add;
  213. ListItem.Caption:='第一列内容';
  214. ListItem.SubItems.Add('第二列内容');
  215. end;
  216. →清空ListView1
  217. ListView1.Items.Clear;
  218. →得到当前被选中行的行的行号以及删除当前行:
  219. For i:= to ListView1.Items.Count- Do
  220. If ListView1.Items[i].Selected then //i=ListView1.Selected.index
  221. begin
  222. ListView1.Items.Delete(i); //删除当前选中行
  223. end;
  224. 当然,ListViewOnSelectItem事件,可以判断选择了哪行,用个全局变量把它赋值出来。
  225. →读某行某列的操作:
  226. Edit1.Text := listview1.Items[i].Caption; //读第i行第1列
  227. Edit2.Text := listview1.Items[i].SubItems.strings[]; //读第i行第2列
  228. Edit3.Text := listview1.Items[i].SubItems.strings[]; //读第i行第3列
  229. 以次类推,可以用循环读出整列。
  230. →将焦点上移一行:
  231. For i:= to ListView1.Items.Count- Do
  232. If (ListView1.Items[i].Selected) and (i>) then
  233. begin
  234. ListView1.SetFocus;
  235. ListView1.Items.Item[i-].Selected := True;
  236. end;
  237. 不过在Delphi6中,ListView多了一个ItemIndex属性,所以只要
  238. ListView1.SetFocus;
  239. ListView1.ItemIndex:=;
  240. 就能设定焦点了。
  241.  
  242. Delphilistview能实现交替颜色么?
  243. procedure TForm1.ListView1CustomDrawItem(
  244. Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  245. var DefaultDraw: Boolean);
  246. var
  247. i: integer;
  248. begin
  249. i:= (Sender as TListView).Items.IndexOf(Item);
  250. if odd(i) then sender.Canvas.Brush.Color:= $02E0F0D7
  251. else sender.Canvas.Brush.Color:= $02F0EED7;
  252. Sender.Canvas.FillRect(Item.DisplayRect(drIcon));
  253. end;
  254.  
  255. 要想随时更改ListView 中某一行的字体颜色,要在ListView OnCustomDrawItem 的事件中书写相关的代码。例如 我想更改选中的某行字体的颜色,则需要在事件中写入下的代码:
  256.  
  257. if item.Index = strtoint(edit1.Text) then //该条件是用于判断是否符合更改字体颜色的行的条件。
  258. Sender.Canvas.Font.Color := clred;

如何设置网格线?

  1. ViewStyle := vsReport;
  2. GridLines := True;

Delphi界面篇之ListView控件的更多相关文章

  1. PyQt学习随笔:QtDesigner ListView控件列表项的初始化

    在QtDesigner中设计的界面中添加ListView控件后,是没办法添加需要在ListView控件中显示的列表项.由于ListView控件只是一个展示列表项的视图控件,实现了界面与数据的分离,其要 ...

  2. C#如何解决对ListView控件更新以及更新时界面闪烁问题

    第一个问题:如何更新ListView控件内容 很多时候运行窗体程序时,由于程序中使用了多线程加之操作不当,所以在对控件操作时会出现下面这样的异常:   这是因为我们在窗体中添加的控件都有属于自己的线程 ...

  3. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

  4. ListView控件的理解——自洽理论

    写在前面的话: *标题中已经说明,是自洽理论.因此,有几率会有理解错误.但是,你不可以因此骂我. -我这个人经不起别人的批评,如果你批评我,我就,我就.... ## <第一行代码>读书笔记 ...

  5. ListView控件--2016年12月9日

    ListView属性 ListView   名称 说明 AccessKey 重写 WebControl.AccessKey 属性. 不支持将此属性设置 ListView 控件.(覆盖 WebContr ...

  6. 读取其他软件listview控件的内容

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. winform listview控件

    ListView控件 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置 ...

  8. WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  9. Android中ListView 控件与 Adapter 适配器如何使用?

    一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...

随机推荐

  1. SVG绘制随机的柱形图+php

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Optimal Value Functions and Optimal Policy

    Optimal Value Function is how much reward the best policy can get from a state s, which is the best ...

  3. 学习mybaits用到的4张表

    items orderdetail orders user

  4. zookeeper+dubbo+demo

    zookeeper下载地址 https://archive.apache.org/dist/zookeeper/ zookeeper安装和使用 windows环境 https://blog.csdn. ...

  5. kmp(多次无重叠匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  6. Codeforces 1162D Chladni Figure(枚举因子)

    这个题好像可以直接暴力过.我是先用num[len]统计所有每个长度的数量有多少,假如在长度为len下,如果要考虑旋转后和原来图案保持一致,我们用a表示在一个旋转单位中有几个长度为len的线段,b表示有 ...

  7. Anaconda Jupyter WinError2:The system cannot find the file specified

    Traceback (most recent call last): File "C:\Users\builder\Miniconda3\Scripts\conda-build-script ...

  8. Centos7.6替换自带的jre安装jdk

    Centos7.6自带jre 1.8,可以作为java运行环境.但如果要编译java程序那就需要jdk,以下介绍如何把自带的jre卸掉并安装jdk 首先要卸载自带的jre PS:由于不同版本的操作系统 ...

  9. BZOJ 5450 轰炸 (强连通缩点+DAG最长路)

    <题目链接> 题目大意: 有n座城市,城市之间建立了m条有向的地下通道.你需要发起若干轮轰炸,每轮可以轰炸任意多个城市.但每次轰炸的城市中,不能存在两个不同的城市i,j满足可以通过地道从城 ...

  10. Jenkins windows 执行批量cmd命令XCOPY 提示'XCOPY' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    由于Jenkins没有配置环境变量造成 打开Jenkins=>Manage Jenkins =>Configure System =>全局属性 新增全局变量 健: Path 值: % ...