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

Grids

How can I add MultiSelect functionality to a grid? by ChrisRChamberlain
Posted: 29 Jan 01 (Edited 4 Apr 10)

If you want to add Multiselect functionality to a grid that emulates Windows Explorer behaviour, try the following:-

Subclass a grid into a suitable class library.

Add 4 new properties to the class,

.lMultiSelect with value of .F.
.nActiveRow with value of 0
.nLastRow with value of 0
.nRecs2Change with value of 0

and 1 new method

.mSelectRecords()

Ensure the .RecordSource of the grid is not indexed, so create it from a SELECT - SQL statement. The .RecordSource also requires an additional logical field, selected.

In the .AfterRowColChange() event of the grid, put

THIS.mSelectRecords()

In the .mSelectRecords() event of the grid, put

LOCAL lcSelected,;
    lcRecordSource

#DEFINE VK_lSHIFT 0x10
&& Relocate to a header file
#DEFINE VK_lCONTROL 0x11
&& Relocate to a header file

DECLARE INTEGER GetKeyState IN WIN32API INTEGER
&& Relocate to where WinAPI calls are declared

WITH THIS
    .nActiveRow    = .ActiveRow
&& Assign value to class property
    lcSelected = .RecordSource + [.selected]
&& Assign value to local variable
    lcRecordSource = .RecordSource
&& Assign value to local variable

    DO CASE
    CASE GetKeyState(VK_lSHIFT)    < 0    ;
            OR GetKeyState(VK_lSHIFT) > 1
&& Check for shift key press

        DO CASE
        CASE .nLastRow > .nActiveRow
&& Last recd below current recd in grid

            .nRecs2Change = .nLastRow - .nActiveRow
&& Calculate no of recds to change

            REPLACE (lcSelected) WITH .T. IN (lcRecordSource)
&& Replace current recd
            FOR i = 1 TO .nRecs2Change
                REPLACE (lcSelected) WITH .T. IN (lcRecordSource)
                SKIP IN (lcRecordSource)
            ENDF

        CASE .nLastRow < .nActiveRow
&& Last recd above current recd in grid

            .nRecs2Change = .nActiveRow - .nLastRow
&& Calculate no of recds to change
            REPLACE  (lcSelected) WITH .T. IN (lcRecordSource)
&& Replace current recd
            
            GO .nLastRow IN (lcRecordSource)
&& Goto the last recd        
            FOR i = 1 TO .nRecs2Change
                REPLACE (lcSelected) WITH .T. IN (lcRecordSource)
                SKIP IN (lcRecordSource)
            ENDF
        ENDC

        .lMultiSelect = .T.

    CASE GetKeyState(VK_lCONTROL) < 0 ;
            OR GetKeyState(VK_lCONTROL) > 1
&& Check for control key press

        REPLACE (lcSelected) WITH .T. IN (lcRecordSource)

        .lMultiSelect = .T.

    OTHERWISE
&& Neither shift or ctrl pressed
        DO CASE
        CASE .lMultiSelect    
            REPLACE (lcSelected) WITH .F. ;
                ALL IN (lcRecordSource)
&& Update all recds
        CASE .nLastRow    # 0
            TRY
                GO .nLastRow IN (lcRecordSource)
            CATCH
                GO BOTTOM IN (lcRecordSource)
            ENDTRY            
            REPLACE (lcSelected) WITH .F. IN (lcRecordSource)    
        ENDCASE

        GO .nActiveRow IN (lcRecordSource)
&& Change new value
        REPLACE (lcSelected) WITH .T. IN (lcRecordSource)

        .lMultiSelect = .F.
    ENDC

    IF RECCOUNT(lcRecordSource) > 0
        DO CASE
&& Set colours according to OS
        CASE UPPER(OS(1)) = [WINDOWS 5.00]
&& Win 2K
            .SetAll([DynamicBackColor]    ,;
                "IIF(&lcSelected        ,;
                    RGB(10,36,106)        ,;
                    RGB(255,255,255))"    ,;
                    [Column])
        CASE UPPER(OS(1)) = [WINDOWS 5.01]
&& Win XP
            .SetAll([DynamicBackColor]    ,;
                "IIF(&lcSelected        ,;
                    RGB(49,106,197)        ,;
                    RGB(255,255,255))"    ,;
                    [Column])
        CASE UPPER(OS(1)) = [WINDOWS 6.00]
&& Vista
            .SetAll([DynamicBackColor]    ,;
                "IIF(&lcSelected        ,;
                    RGB(51,153,255)        ,;
                    RGB(255,255,255))"    ,;
                    [Column])
        CASE UPPER(OS(1)) = [WINDOWS 6.01]
&& Windows 7
            .SetAll([DynamicBackColor]    ,;
                "IIF(&lcSelected        ,;
                    RGB(51,153,255)        ,;
                    RGB(255,255,255))"    ,;
                    [Column])
        ENDCASE

        .SetAll([DynamicForeColor]    ,;
&& All OS
            "IIF(&lcSelected        ,;
                RGB(255,255,255)    ,;
                RGB(0,0,0))"        ,;
                [Column])
    
        .nLastRow = .nActiveRow
&& Mark current row for next time through
    ENDIF
ENDWITH


*!*    *!*    *!*    *!*    *!*    *!*    *!*    *!*    *!*    *!*    *!*    

Left mouse click will select the current record only, shift + click followed by shift + click will select a block of records, and ctrl + click will select individual records, all selected records being highlighted.

Scrolling with the arrow keys does not select records.

You can programatically determine if there are multiple selected records with:-

IF THISFORM.grid1.lMultiSelect

    *!* Code
ENDI

You now have a scope clause available for copying, printing, deleting etc, expressed as:-

REPORT FORM reportname.frx FOR CURSORNAME.selected

Have fun

Chris pc2
PDFcommander.com
PDFcommander.co.uk

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