[轉]Delphi 常用控制項之TlistView總結

來源:互聯網
上載者:User

標籤:

Delphi 常用控制項之TlistView總結

要用到的(大部分是TListView的ViewStyle屬性設為vsReport下的):

TListView 組件的常用屬性

(3)Column 屬性

此屬性可以訪問列表視圖中每一欄,Index 指定欄的序號。此屬性僅當ViewStyle 屬性設為vsReport時有效。

(4)ColumnClick 屬性

如果此屬性設為True,欄的表頭(如果有的話)將同時作為按鈕用,按下這些按鈕,將觸發OnColumnClick 事件,通常用於對列表視圖中的項進行重新排序。

(10)RowSelect 屬性

在ViewStyle 屬性設為vsReport 的前提下,如果把此屬性設為True,使用者可以一次選擇一整行。

(11)Selected 屬性

此屬性用於設定返回列表視圖中被選擇的項。如果有多項被選擇,返回的是當前有輸入焦點的項。

(14)ViewStyle 屬性

ListView 控制項作為一個可以顯示表徵圖或者子項的清單控制項,它最重要的屬性就是 ViewStyle 屬性,該屬性決定了以哪種視圖模式顯示控制項的項,這四種視圖模式分別如下:

1. vsIcon:大表徵圖視圖模式,在項的文本旁顯示大的表徵圖,在控制項寬度足夠的情況下,項是優先以平行排列的,排列不完的則自動換行顯示在新行中。這個屬性要和TListView的LargeImages屬性一起使用;

2. vsSmallIcon:小表徵圖視圖模式,與大表徵圖模式一樣,但是顯示的是小的表徵圖;要和TListView的SmallImages屬性一起使用;

3. vsList:列表視圖模式,可以顯示小表徵圖,但是項是垂直排列的,只顯示單列,不能進行拖放操作;

4. vsReport:詳細資料視圖模式,是最豐富的選項,可以基本實現DBGrid的所有功能;它不但允許您查看項,還允許您查看為各項指定的任何子項。各項在網格中顯示,它們垂直排列且其子項會顯示在列中(帶有列標題)。對應地,只有在Details 視圖模式中起作用的控制項屬性為:GridLines 和 FullRowSelect,GridLines 屬性指示在包含控制項中項及其子項的行和列之間是否顯示網格線。FullRowSelect 屬性指示單擊某項是否選擇其所有子項(即整行選中)

Columns屬性是用來設定有哪些列,這個可以在設計時事先設定,也可以在程式中設定:ListView1.Columns.Items[0].Caption:= ‘列1‘;

ListView1.Columns.Items[1].Caption:= ‘列2‘;//添加列

ListView1.Columns.Items[2].Caption:= ‘列3‘;

ListView1.Columns.Items[2].Width:=300; //這裡設定列的寬度

TListView的Items屬性也是很重要的,用來向ListView中添加資料, Items屬性是一個TListItem類,在進行資料添加時一般先定義個TListItem類。

 

TListView 組件的常用事件

(1)OnColumnClick 事件

當使用者單擊欄的表頭時,將觸發該事件。這樣就可以對項進行排序。在OnColumnClick 事件中有一個TListColumn 類型的參數Column,此參數返回使用者單擊的欄,利用Column 參數可以對使用者選中的欄進行訪問及設定。

(5)OnSelectItem 事件

當列表視圖中一個項被選擇時,將觸發這個事件。OnSelectItem 事件中有一個TListItem 類型的參數Item,利用此參數可以對使用者編輯的項進行訪問;OnSelectItem 事件中還有一個Boolean 類型的參數Selected。每次選擇一個新項時將兩次觸發此事件,一次是釋放以前被選擇的項,Selected 參數取值為False,代表釋放Item 參數指定的項;另外一次是選擇新的項,Selected 參數取值為True,代表選擇Item 參數指定的項。

 

TListView 組件常用的方法

 

TlistView組件的使用方法

引用CommCtrl單元

(1)賦值

with ListView1.Items.Add do

begin

Caption:=caption; //添加第一項

SubItems.add(aaaaa); //

SubItems.add(1234);

SubItems.add(1234);

end;

with ListView1 do

begin

ListItem:=Items.Add;

ListItem.Caption:=‘第一列內容‘;

ListItem.SubItems.Add(‘第二列內容‘);

end;

(2)取值

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列

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; 清除資料

(6)插入一列

DestItem := lvList.Items.Insert(CurItem.Index );//在當前列前面插入一列

DestItem := lvList.Items.Insert(CurItem.Index + 1);//在當前列後面插入一列

然後對DestItem按(1)中的方法賦值,注意對於每條記錄Item,其Item.SubItems.Count可能不同。

(7)刪除一列

lvList.Items.Delete(tItem.Index);//刪除當前列

lvList.Items.Delete(tItem.Index + 1);//刪除當前列後面一列,要先判斷其存在

//這是個通用的過程

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組件常用技巧

實現不同行顏色不一樣

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);

begin //OwnerDraw應該設定為False

if Item.Index mod 2 =0 then

Sender.Canvas.Brush.Color :=clSkyBlue

else

Sender.Canvas.Brush.Color :=clWhite;

end;

實現單擊列表標題進行排序

function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall;

begin

if ColumnIndex = 0 then

Result := CompareText(Item1.Caption,Item2.Caption)

else

Result := CompareText(Item1.SubItems[ColumnIndex-1],Item2.SubItems[ColumnIndex-1])

end;

procedure TFrmMain.TypeLvColumnClick(Sender: TObject; Column: TListColumn);

begin

TypeLv.CustomSort(@CustomSortProc,Column.Index);

end;

判斷當前單擊的哪一行

GetCursorPos(p);

p := lvList.ScreenToClient(p);

CurItem := lvList.GetItemAt(p.X, p.Y);

ARect:= CurItem.DisplayRect(drBounds);

根據存入的Data找到指定的Item

curItem := lvRoomList.FindData(0, AData, True, False);

在vsReport模式下第一列插入兩個表徵圖

lvList.SmallImages,lvListStateImages分別關聯一個TimageList對象,TimageList對象對象中存入所有要顯示的表徵圖,對與TlistView 的vsReport模式下的每一條記錄TlistItem對象tItem,可分別通過tItem.ImageIndex和tItem.StateIndex來控制顯示TimageList對象中的哪個表徵圖,一般tItem.StateIndex顯示的表徵圖在前,tItem.ImageIndex顯示的表徵圖在後,若要判斷單擊事件是按一下滑鼠哪個表徵圖的,可通過GetCursorPos(p);獲得滑鼠座標後判斷橫座標即P.X判斷。

 

[轉]Delphi 常用控制項之TlistView總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.