// Extra functions that can be applied to a CREATE CONTROL LISTBOX

// Also see the SET LISTBOX TO CA-dBFast command.

// Windows API function call
declare pfunc SendMessage returning integer in dll user.exe

#define LB_ADDSTRING						1025
#define LB_INSERTSTRING					1026
#define LB_DELETESTRING					1027
#define LB_RESETCONTENT					1029
#define LB_SETSEL								1030
#define LB_SETCURSEL						1031
#define LB_GETSEL								1032
#define LB_GETCURSEL						1033
#define LB_GETTEXT							1034
#define LB_GETTEXTLEN						1035
#define LB_GETCOUNT							1036
#define LB_SELECTSTRING					1037
#define LB_DIR									1038
#define LB_GETTOPINDEX					1039
#define LB_FINDSTRING						1040
#define LB_GETSELCOUNT					1041
#define LB_GETSELITEMS						1042
#define LB_SETTABSTOPS					1043
#define LB_GETHORIZONTALEXTENT		1044
#define LB_SETHORIZONTALEXTENT		1045
#define LB_SETTOPINDEX					1048
#define LB_GETITEMRECT					1049
#define LB_GETITEMDATA						1050
#define LB_SETITEMDATA						1051
#define LB_SETCARETINDEX					1055
#define LB_GETCARETINDEX				1056
#define LB_FINDSTRINGEXACT				1059

#define DDL_READWRITE						0
#define DDL_READONLY						1
#define DDL_HIDDEN							2
#define DDL_SYSTEM							4
// DDL_DIRECTORY gets both files, and directories
#define DDL_DIRECTORY						16
#define DDL_ARCHIVE							32


// SAVE TO for the listbox
sRetVar = ''

// Create the listbox
create control listbox 'ListBox01' at 0,0 save to sRetVar contents 'Listbox' size 10,40

// get the window handle for the listbox
hWnd = HWND('ListBox01')

@ 11,0 say

// String to add to listbox
lpszText = 'Added Text 1'
// Add the text to the listbox, returns the zero based index of the added text
//? SendMessage(toint(hwnd), toint(LB_ADDSTRING), toint(0), lpszText)
lpszText = 'Added Text 2'

sLength = 0
set cursor to wait

// Able to add up to 64K of text to the listbox.
// However, it is somewhat slow.
for loop = 1 to 2300
	lpszText = alltrim(str(loop)) + ' List Box item number'
	sLength = sLength + len(lpszText)
	SendMessage(toint(hwnd), toint(LB_ADDSTRING), toint(0), lpszText)
	chkevent()
next loop
set cursor to arrow

? slength
wait

// Find a string in the listbox contents
lpszText = '9'
// Returns the index number of the found string, or -1 if no match
// Returned index depends on if the listbox is sorted
// This is a softseek; it will find a match if the item starts with lpszText
// The 3rd parameter is the index number to start the seek; -1 to start at the top
? SendMessage(toint(hwnd), toint(LB_FINDSTRING), toint(-1), lpszText)

wait 'After soft seek'

// Find a string in the listbox contents
lpszText = '9 List Box item number'
// Returns the index number of the found string, or -1 if no match
// Returned index depends on if the listbox is sorted
// This is an exact seek; only an exact match will return the index number
// The 3rd parameter is the index number to start the seek; -1 to start at the top
? SendMessage(toint(hwnd), toint(LB_FINDSTRINGEXACT), toint(-1), lpszText)

wait 'After hard seek'

// Remove all items from the listbox
? SendMessage(toint(hwnd), toint(LB_RESETCONTENT), toint(0), tolong(0))

wait

// Zero based index of item to delete
nLBIndex = 1
// Delete the item from the listbox, returns the remaining number of items
// Returns -1 if nLBIndex is greater than the number of items
//? SendMessage(toint(hwnd), toint(LB_DELETESTRING), toint(nLBIndex), tolong(0))

//wait 'Number of strings left.'

// Zero based index of item to insert
nLBIndex = 1
lpszText = 'Added Text 3'
// Insert the item into the listbox, returns the index of the point of insertion
// Note: inserted strings will not be sorted
//? SendMessage(toint(hwnd), toint(LB_INSERTSTRING), toint(nLBIndex), lpszText)

wait

// Get the number of items in the listbox
? SendMessage(toint(hwnd), toint(LB_GETCOUNT), toint(0), tolong(0))

wait

chdir 'c:\'
// Set the file match
lpszFileName = '*.*'
// Fill the listbox with file names from the current directory
? SendMessage(toint(hwnd), toint(LB_DIR),toint(DDL_DIRECTORY), lpszFileName)
chdir 'd:\dbf20b\progs'
wait
