Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simulating checkboxes in a listbox

ListBox

Simulating checkboxes in a listbox

by  craigsboyd  Posted    (Edited  )
Slighthaze [color blue]NULL[/color]
[img http://www.sweetpotatosoftware.com/ttimages/listboxcheckbox.gif]
First of all, allow me to give credit to the book "1001 Things You Wanted to Know About Visual Foxpro" and its Authors: Marcia Akins, Andy Kramek & Rick Schummer. This is where I first learned the techniques that make this possible.

In order to show checkboxes in a listbox and hook the whole thing up to a table you need to use the picture property array (among other things) of the listbox. They aren't really checkboxes, they're icons. In fact the ones I'm using are more like lights (I wanted to provide an example that you could run right out of the box). You can use whatever images you want...find a couple of really good images of checkboxes that are checked and unchecked and just replace the appropriate form properties.

The listbox has a cursor for a rowsource and one of the "checked" field is used to keep track of which records are checked/unchecked. You could even take it a step further and change the Checked field to numeric and have a Multi-State checkbox with values 0, 1, & 2 and use three images. (Cut-N-Paste the code below into a prg file and run it from within VFP)

PUBLIC oForm

oForm = CREATEOBJECT("clsListCheckBox")

oForm.VISIBLE = .T.

READ EVENTS

DEFINE CLASS clsListCheckBox AS FORM

TOP = 1
LEFT = 0
HEIGHT = 473
WIDTH = 287
DOCREATE = .T.
CAPTION = "Listbox With Checkboxes"
WINDOWSTATE = 0
NAME = "clsListCheckBox"
AlwaysOnTop = .T.
CheckIcon = HOME() + "Graphics\Icons\Misc\MISC15.ICO"
Uncheckicon = HOME() + "Graphics\Icons\Misc\MISC13.ICO"
SHOWWINDOW = 2

ADD OBJECT list1 AS LISTBOX WITH ;
HEIGHT = 408, ;
LEFT = 12, ;
SORTED = .T., ;
TOP = 48, ;
WIDTH = 264, ;
NAME = "List1", ;
ROWSOURCETYPE = 2, ;
ROWSOURCE = "ListCheck"

PROCEDURE LOAD
LOCAL nCount, nCount2, nWordLength, sItem, nUpper, nLower
nUpper = 90 &&ASCII
nLower = 65 &&ASCII
CREATE CURSOR ListCheck (MyEntry c(35), Checked L)
FOR nCount = 1 TO 250
sItem = ""
nWordLength = INT((35) * RAND( ) + 1)
FOR nCount2 = 1 TO nWordLength
sItem = sItem + CHR(INT((nUpper - nLower + 1) * RAND( ) + nLower))
ENDFOR
INSERT INTO ListCheck (MyEntry, Checked) VALUES(sItem, .F.)
NEXT
ENDPROC

PROCEDURE Unload
USE IN SELECT("ListCheck")
CLEAR EVENTS
ENDPROC

PROCEDURE ListSetup
THISFORM.LOCKSCREEN = .T.
LOCAL nListCount
nListCount = 1
SELECT ListCheck
SCAN ALL
IF ListCheck.Checked
THIS.list1.PICTURE(nListCount) = THISFORM.CheckIcon
ELSE
THIS.list1.PICTURE(nListCount) = THISFORM.Uncheckicon
ENDIF
nListCount = nListCount + 1
ENDSCAN
THISFORM.LOCKSCREEN = .F.
ENDPROC

PROCEDURE SetCheck
LOCAL nListIndex
nListIndex = THIS.list1.LISTINDEX
IF nListIndex > 0
GO nListIndex IN "ListCheck"
IF ListCheck.Checked
THIS.list1.PICTURE(nListIndex) = THISFORM.Uncheckicon
ELSE
THIS.list1.PICTURE(nListIndex) = THISFORM.CheckIcon
ENDIF
REPLACE ListCheck.Checked WITH !ListCheck.Checked
ENDIF
ENDPROC

PROCEDURE list1.GOTFOCUS()
IF DODEFAULT()
THISFORM.ListSetup()
ENDIF
ENDPROC

PROCEDURE list1.CLICK()
IF LASTKEY() = 13
THISFORM.SetCheck()
ENDIF
ENDPROC

PROCEDURE list1.KEYPRESS(nKeyCode, nShiftAltCtrl)
IF nKeyCode = 13 OR nKeyCode = 32
THISFORM.SetCheck()
ENDIF
ENDPROC

ENDDEFINE
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top