INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

ListBox

Simulating checkboxes in a listbox by craigsboyd
Posted: 18 Jul 03 (Edited 18 Feb 05)

Slighthaze NULL

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

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close