TListBox
Source: source/classes/listbox.prg
Inherits from: TControl
TListBox wraps the standard Windows list box control (LISTBOX). It displays a scrollable list of items from which the user can select one or multiple entries. TListBox supports owner-drawn items with bitmaps, drag-and-drop reordering, multiple-selection modes, keyboard navigation, and item-level tooltips. It is the standard single-column list control in FiveWin.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
aItems | Array | Array of character strings displayed in the list box. |
aBitmaps | Array | Array of bitmap handles or resource names for owner-drawn bitmap items. Each entry corresponds to an item in aItems. |
bSetGet | Block | Data-binding block. When evaluated with no arguments, returns the selected item value; with one argument, selects that item. |
lOwnerDraw | Logical | If .T., the list box is owner-drawn, allowing custom item rendering via bDrawItem. |
bDrawItem | Block | Custom drawing code block for owner-drawn items: { |nItem, hDC, aRect, nState, oLbx| ... }. |
lMultiSelect | Logical | If .T., multiple items can be selected at once. |
lDrag | Logical | Enable mouse drag-and-drop reordering of list items. |
bChange | Block | Evaluated when the selection changes: { |nItem, oLbx| ... }. |
bValid | Block | Validation block evaluated when the list box loses focus. |
nAt | Numeric | Index of the currently selected item (1-based). 0 means no selection. |
Methods
| Method | Description |
|---|---|
New( nRow, nCol, bSetGet, aItems, nW, nH, oWnd, lMultiSel, lDrag, lOwner, oFont, bDraw, lPixel, lDesign, oIcon, cMsg, lUpdate, bChange, bValid, ... ) | Create a new list box. aItems is the initial item array. bSetGet binds the selected value to a variable. |
Add( cItem, nAt ) | Add a new item. If nAt is omitted, the item is appended at the end. If specified, it is inserted at that position. |
Del( nAt ) | Delete the item at position nAt (1-based). |
Modify( cItem, nAt ) | Replace the item at position nAt with new text cItem. |
Select( nItem ) | Programmatically select the item at position nItem. |
SetItems( aItems ) | Replace all items in the list box with a new array of strings. |
GetPos() | Return the index of the currently selected item (1-based). Returns 0 if no item is selected. |
GetSel() | Return the selected item text as a string. |
nCount() | Return the total number of items in the list box. |
Clear() | Remove all items from the list box. |
End() | Destroy the control. |
Commands: @ ... LISTBOX
@ nRow, nCol LISTBOX oLbx ;
ITEMS aItems ;
[ VAR uVar ] ;
[ SIZE nW, nH ] ;
[ OF oDlg ] ;
[ MULTISELECT ] ;
[ DRAG ] ;
[ ON CHANGE bChange ] ;
[ ON VALID bValid ] ;
[ BITMAPS aBitmaps ] ;
[ OWNERDRAW ]
Example: 3-Item Listbox
#include "FiveWin.ch"
function Main()
local oWnd, oLbx, cChoice := ""
DEFINE WINDOW oWnd TITLE "TListBox Demo" SIZE 400, 300
@ 20, 20 LISTBOX oLbx ;
ITEMS { "Apples", "Oranges", "Bananas" } ;
SIZE 200, 150 OF oWnd ;
ON CHANGE MsgInfo( "Selected: " + oLbx:GetSel() )
@ 200, 20 BUTTON "Get Selection" SIZE 120, 30 OF oWnd ;
ACTION MsgInfo( "Item " + Str( oLbx:GetPos() ) + ;
": " + oLbx:GetSel() )
ACTIVATE WINDOW oWnd CENTERED
return nil
Notes
- TListBox inherits from TControl. All standard control methods are available.
- Use
lMultiSelect := .T.(or theMULTISELECTclause) to allow the user to select multiple items using Ctrl+click or Shift+click. - The
bDrawItemblock enables custom rendering. WhenlOwnerDrawis.T., the list box becomes owner-drawn and thebDrawItemblock is called for each visible item during paint. - Item bitmaps (
aBitmaps) provide a simple icon next to each item without needing owner-draw. Each element is a bitmap handle or resource name. - Drag-and-drop reordering is enabled via the
DRAGclause. Items can be dragged to new positions within the list. - For keyboard navigation, the list box handles Up/Down arrows, PageUp/PageDown, and the Home/End keys automatically.