delphi -----TListView的用法

层次关系:

TListView:
           ->Columns:
           ->Items  : -->TListItems:   
                      -->Clear:                        
                      -->BeginUpdate:
                      -->Add:      --->TListItem:  ---->Caption:
                                                   ----> SubItems:-->TStrings:->Add
TListView:代表整个列表,所有的行和列的属性方法的集合
->Columns:所有列
->Items  :所有行,TListItems类型
       -->TListItems: 所有行即条目的属性方法的集合 
       -->Clear: 移除所有的条目                      
       -->BeginUpdate: 更新所有的条目
       -->Add: 添加新的条目 ,TListItem类型  
            --->TListItem: 一行或者条目的属性方法的集合
            --->Caption:一个条目的名称
            ---> SubItems:一个条目的子条目,TStrings类型
                   -->TStrings:一个字符串的集合
                        ->Add:添加一个字符串

一、属性 

1.ListView主要属性

(1)ViewStyle属性
ViewStyle属性用于选择数据项的4种显示方式,因此该属性有4个选项值:vsIcon大图标、vsSmallIcon小图标、vsList列表、vsReport详细列表

区别参考资料:http://www.cnblogs.com/del/archive/2009/01/04/1368187.html

vsReport:

1)可以像grid一样展示

2)如果要有线条..设gridLines = true 

 

二、添加、修改、删除

实例:

 添加

  加载到列表

item := ListView1.Items.Add;//增加一行
item.Caption := cbb_inputs.Text;
item.SubItems.Add(cbb_VAL.Text);
item.SubItems.Add(edt_inputDev.Text);
item.SubItems.Add(edt_inputch.Text);
item.SubItems.Add(edt_CONSTANTS.Text);

最好加个BeginUpdate , EndUpdate如下

lv_Item.Items.BeginUpdate;
try
  for j := 0 to ItemArr.Size-1 do
  begin
    Itemsnum:=ItemArr.items[j].Value;
    ddevcondition:=JSON(['_id',Itemsnum]);
    ddev:=FMongoWire.Get(devCol,ddevcondition);
    li:=lv_Item.Items.Add;
    li.Caption:=Itemsnum;
    li.SubItems.Add(VarToStr(ddev['name']));//siType
  end;
finally
  lv_Item.Items.EndUpdate;
end;

修改

  将列表数据传EDIT中  

  if ListView1.Selected=nil then raise Exception.Create('请选择要修改的数据!');
  cbb_inputs.Text:=ListView1.Selected.Caption;
  stype:= ListView1.Selected.SubItems.Strings[0];
  cbb_VAL.Text:=stype;
  edt_inputDev.Text:=stype;
  if(stype='VALUE')then
  begin
    edt_inputDev.Text:=ListView1.Selected.SubItems.Strings[1];
    edt_inputch.Text:=ListView1.Selected.SubItems.Strings[2];
    edt_CONSTANTS.Text:='';
    btn_dev.Enabled:=True;
    btn_ch.Enabled:=True;
  end
  else if (stype='CONSTANTS') then
  begin
    edt_inputDev.Text:='';
    edt_inputch.Text:='';
    edt_CONSTANTS.Enabled:=true;
    edt_CONSTANTS.Text:=ListView1.Selected.SubItems.Strings[3];
end;

 

  加载到列表

  ListView1.Selected.Caption:=cbb_inputs.Text;//选择的行
  ListView1.Selected.SubItems.Strings[0]:=cbb_VAL.Text;

  if(ListView1.Selected.SubItems.Strings[0]='VALUE') then
  begin
    ListView1.Selected.SubItems.Strings[1]:=edt_inputDev.Text;
    ListView1.Selected.SubItems.Strings[2]:=edt_inputch.Text;
  end
  else if(ListView1.Selected.SubItems.Strings[0]='CONSTANTS') then
    ListView1.Selected.SubItems.Strings[3]:=edt_CONSTANTS.Text;

删除

if listview1.Selected= nil then exit ;

ListView1.Selected.Delete;

 

判断行记录是否已存在

首先介绍一下TlistView中FindCaption的用法:

function FindCaption(StartIndex: Integer; Value: string; Partial, Inclusive, Wrap: Boolean): TListItem;

这个函数用于查找listview中caption为某个值的记录,返回值为这个记录对应的ListItem,如果不存在这个记录,则返回nil.

当caption的长度大于255时,即使listview中存在这条记录,该函数亦会返回nil,

if( ListView1.FindCaption(0, Trim(cbb_inputs.Text), True, True, True)=nil)then
begin
ListView1.Selected.Caption:=cbb_inputs.Text;
ListView1.Selected.SubItems.Strings[0]:=cbb_VAL.Text;

if(ListView1.Selected.SubItems.Strings[0]='VALUE') then
begin
ListView1.Selected.SubItems.Strings[1]:=edt_inputDev.Text;
ListView1.Selected.SubItems.Strings[2]:=edt_inputch.Text;
end
else if(ListView1.Selected.SubItems.Strings[0]='CONSTANTS') then
ListView1.Selected.SubItems.Strings[3]:=edt_CONSTANTS.Text;
end
else begin
ShowErrMsg('已存在【'''+Trim(cbb_inputs.Text)+''' 】');
Exit;
end;

 

修改时,判断行记录是否已存在

selectNum:=ListView1.Selected.index;//当前行的序号
scaption:= Trim(cbb_inputs.Text);
For i:=0 to ListView1.Items.Count-1 Do
begin
  if(i<>selectNum)then
  begin
    if(scaption= ListView1.Items[i].Caption ) then
    begin
      ShowErrMsg('已存在输入量【'''+Trim(cbb_inputs.Text)+''' 】');
      Exit
    end;
  end;
end; 

 

判断某列值是否设置

For i:=0 to ListView1.Items.Count-1 Do
begin
  if(listview1.Items[i].SubItems.strings[4]='√') then
  begin
    bzcb:=True;
    Break;
  end;
end;
if(bzcb=False)then
begin
  ShowErrMsg('请设置主设备');
Exit;

已有个数: ListView1.Items.Count

 

posted on 2017-08-30 08:25  michellexiaoqi  阅读(4834)  评论(0编辑  收藏  举报