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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM