TListView
Windows ListView common control with icons, details, groups, and tile views
Fonte: source/classes/tlistvie.prg | Parent: TControl
Overview
TListView wraps the Windows SysListView32 common control. Unlike TXBrowse (which is designed for database record browsing), TListView is designed for displaying lists of items with icons -- similar to Windows Explorer's file listing. It supports five view modes, item grouping, image lists, and item selection.
Architecture
graph TD
TControl --> TListView
TListView --> IL_NORMAL["oImageList
(Normal Icons)"] TListView --> IL_SMALL["oImageList
(Small Icons)"] TListView --> GROUPS["aGroups[]
Item Groups"] TListView --> ITEMS["aItems[]
List Items"] subgraph "View Modes" ICON["LV_VIEW_ICON
Large icons"] DETAILS["LV_VIEW_DETAILS
Column details"] SMALL["LV_VIEW_SMALLICON
Small icons"] LIST["LV_VIEW_LIST
Simple list"] TILE["LV_VIEW_TILE
Tile layout"] end TListView --> ICON TListView --> DETAILS TListView --> SMALL TListView --> LIST TListView --> TILE style TListView fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style DETAILS fill:#1c2129,stroke:#3fb950,stroke-width:1px,color:#e6edf3
(Normal Icons)"] TListView --> IL_SMALL["oImageList
(Small Icons)"] TListView --> GROUPS["aGroups[]
Item Groups"] TListView --> ITEMS["aItems[]
List Items"] subgraph "View Modes" ICON["LV_VIEW_ICON
Large icons"] DETAILS["LV_VIEW_DETAILS
Column details"] SMALL["LV_VIEW_SMALLICON
Small icons"] LIST["LV_VIEW_LIST
Simple list"] TILE["LV_VIEW_TILE
Tile layout"] end TListView --> ICON TListView --> DETAILS TListView --> SMALL TListView --> LIST TListView --> TILE style TListView fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style DETAILS fill:#1c2129,stroke:#3fb950,stroke-width:1px,color:#e6edf3
View Modes
| Constant | Value | Description |
|---|---|---|
LV_VIEW_ICON | 0 | Large icon view -- items displayed as large icons with label below |
LV_VIEW_DETAILS | 1 | Details/report view -- items in rows with multiple columns |
LV_VIEW_SMALLICON | 2 | Small icon view -- items as small icons arranged freely |
LV_VIEW_LIST | 3 | List view -- items as small icons in a vertical list |
LV_VIEW_TILE | 4 | Tile view -- items displayed as tiles with icon and multi-line text |
Key Properties
| Property | Type | Description |
|---|---|---|
aPrompts | Array | Array of text labels for the initial items |
aItems | Array | Runtime array of all inserted items (maintained internally) |
aGroups | Array | Array of group definitions for group view |
nOption | Numeric | Currently selected item index (1-based) |
nGroups | Numeric | Number of groups inserted (auto-incremented) |
bAction | Block | Code block executed on item activation (double-click/Enter) |
bClick | Block | Code block executed on single click |
Key Methods
| Method | Description |
|---|---|
New( nTop, nLeft, aPrompts, bAction, oWnd, nClrFore, nClrBack, lPixel, lDesign, nWidth, nHeight, cMsg ) | Constructor. Creates the ListView at the given position. |
ReDefine( nId, oWnd, bAction ) | Redefine from a dialog resource. |
InsertItem( nImageIndex, cText, nGroup ) | Insert a new item with an image index, label, and optional group assignment. |
InsertGroup( cText ) | Insert a new group header with the given label. |
DelItem( nItem ) | Delete the item at 1-based index nItem. |
GetItem( nItem ) | Retrieve item data at the given index. |
FindItem( cText ) | Search for an item by its text label. |
SetItemSelect( nItem ) | Programmatically select an item. |
SetStyle( nStyle ) | Change the view mode (pass one of the LV_VIEW_* constants). |
SetImageList( oImageList, nType ) | Assign an image list. nType: LVSIL_NORMAL (0), LVSIL_SMALL (1), LVSIL_STATE (2). |
SetIconSpacing( x, y ) | Set the spacing between icons in icon view. |
SetHotItem( nItem ) | Set the hot-tracked item. |
EnableGroupView() | Enable group view mode so group headers are visible. |
Command Syntax
@ nRow, nCol LISTVIEW oLvw ;
[ OF oWnd ] ;
[ PROMPTS cItem1, cItem2, cItem3 ] ;
[ ON CHANGE bAction ] ;
[ SIZE nWidth, nHeight ] ;
[ PIXEL ] ;
[ COLOR nClrFore, nClrBack ] ;
[ MESSAGE cMsg ]
REDEFINE LISTVIEW oLvw ;
ID nId ;
[ OF oWnd ] ;
[ ON CHANGE bAction ]
ListView Lifecycle
sequenceDiagram
participant App
participant LV as TListView
participant Win as SysListView32
App->>LV: @ row,col LISTVIEW oLvw PROMPTS ...
LV->>Win: Create("SysListView32")
LV->>Win: InsertGroup("default")
loop For each prompt
LV->>Win: InsertItem(n, cText)
end
App->>LV: SetImageList(oImgList, LVSIL_NORMAL)
LV->>Win: SendMessage(LVM_SETIMAGELIST)
App->>LV: InsertGroup("Category A")
LV->>Win: LVInsertGroup()
App->>LV: InsertItem(0, "New Item", nGroup)
LV->>Win: LVInsertItem()
App->>LV: SetStyle(LV_VIEW_DETAILS)
LV->>Win: LVSetStyle()
Win-->>LV: LVN_ITEMCHANGED
LV-->>App: Eval(bAction)
Code Examples
Example 1: Basic ListView with Icons
#include "FiveWin.ch"
FUNCTION TestListView()
LOCAL oWnd, oLvw, oImgList
DEFINE WINDOW oWnd TITLE "ListView Demo"
// Create a 32x32 image list
oImgList := TImageList():New( 32, 32 )
oImgList:AddMasked( TBitmap():Define( "FOLDER",, oWnd ), CLR_MAGENTA )
oImgList:AddMasked( TBitmap():Define( "FILE",, oWnd ), CLR_MAGENTA )
@ 0, 0 LISTVIEW oLvw OF oWnd ;
PROMPTS "Documents", "Pictures", "Music", "Videos" ;
ON CHANGE { || MsgInfo( "Selected: " + Str( oLvw:nOption ) ) } ;
SIZE 400, 300 PIXEL
oLvw:SetImageList( oImgList, 0 ) // LVSIL_NORMAL
ACTIVATE WINDOW oWnd
RETURN NIL
Example 2: Details View with Groups
#include "FiveWin.ch"
FUNCTION TestListViewGroups()
LOCAL oWnd, oLvw
DEFINE WINDOW oWnd TITLE "ListView Groups"
@ 0, 0 LISTVIEW oLvw OF oWnd SIZE 500, 400 PIXEL ;
ON CHANGE { || ProcessItem( oLvw:nOption ) }
// Switch to details view
oLvw:SetStyle( 1 ) // LV_VIEW_DETAILS
// Insert groups
oLvw:InsertGroup( "Contacts" )
oLvw:InsertGroup( "Suppliers" )
// Enable group headers
oLvw:EnableGroupView()
// Insert items into groups
oLvw:InsertItem( 0, "John Smith", 0 ) // Group 0 = default
oLvw:InsertItem( 0, "Jane Doe", 1 ) // Group 1 = Contacts
oLvw:InsertItem( 0, "Acme Corp", 2 ) // Group 2 = Suppliers
oLvw:InsertItem( 0, "Widget Inc", 2 )
ACTIVATE WINDOW oWnd
RETURN NIL
Example 3: Switching View Modes
// Add a combobox to switch view modes
@ 0, 0 COMBOBOX oCbx ITEMS { "Large Icons", "Details", ;
"Small Icons", "List", "Tiles" } ;
OF oWnd SIZE 150, 200 PIXEL ;
ON CHANGE { || oLvw:SetStyle( oCbx:nAt - 1 ) }
Tips
- For database browsing with sorting, filtering, and editing, use TXBrowse instead.
TListViewis best suited for icon-based selection (file browsers, image galleries, option lists).- Call
EnableGroupView()after inserting groups to make group headers visible. - The
nImageIndexparameter inInsertItem()is 0-based (matching the image list index). - Use
SetIconSpacing()to control the gap between items in icon view mode.
Veja Também
- TControl -- parent class
- TXBrowse -- data-aware grid browser
- TTreeView -- hierarchical tree control
- Resources (GDI) -- TImageList for icons