Grid Tutorial 1- Standard Grid Using SENDMSG() Functions


For Visual PRO/5 Grid Tutorials, see Data-Aware Grid Tutorial, Standard Grid Control Tutorial 1: Display-Only Grid and Standard Grid Tutorial 2: User-Modifiable Grid.

Suggested Procedures for Using the BBj Grid Control with SENDMSG Functions:

  1. Create the basic grid control using either a resource file (ResBuilder and the 'RESOURCE' mnemonic) or the 'GRID' mnemonic. Set the grid to initially invisible.

  2. Define various grid attributes using SENDMSG() functions. Set up default styles per column using setup grid and new SENDMSG() Functions that set default values per column.

  3. Set cell text using SENDMSG() Function 109 or 22. If the data has a large amount of rows and/or columns, setting cell text in response to the table update event may still be faster since it will not have to initialize the data for all cells at once.

  4. Use Set Update Cached Cells - SENDMSG() Function 110 to turn on the option to not send notify 22 when cells already contain data. This will reduce the number of table update events sent from the grid.

  5. Set the editability of the grid using SENDMSGs 106-108. If grid editing is desired for all cells, nothing needs to be done.

  6. Set drag and drop option if desired using Set Drag Accept - SENDMSG() Function 33.

  7. Show grid.

  8. Register Callbacks for handling events or use read record to read events. This tutorial will use read record. No handling of Notify messages is required to manage data, editing, or drag and drop when the grid is in use, although all the notify events are supported.

The following tutorial will create a grid that displays weather information for a week.

  1. The grid will be editable if the editable check box is checked and editing will begin when a cell is double-clicked. If the editable check box it is checked off, the program will display a message box showing the cell contents if a cell is double-clicked.

  2. Setup window and other controls:

    Open SYSGUI Channel and Window

    REM Open SYSGUI Channel and Window
    SYSGUI=UNT
    OPEN (SYSGUI)"X0"
    REM create window with event mask for check event
    PRINT (SYSGUI)'WINDOW'(100,100,600,300,"Grid Tutorial 1",$00010003$,$02000000$)
    
    REM Initialize variables
    GRID=100
    COLID=101
    ROWID=102
    NUMROW=7
    NUMCOL=9
    EDITABLE=1; REM option to set grid editable
    
    PRINT (SYSGUI)'TEXT'(97,10,5,150,25,"Weather Forecast",$$)
    PRINT (SYSGUI)'FONT'(97,"",12,DEC($01$))
    PRINT (SYSGUI)'TEXT'(98,160,10,200,25,"Temperature in degrees Fahrenheit",$$)
    
    REM Display a checkbox to allow Grid editing to be toggled
    CHECK_FLAGS$=$$
    IF EDITABLE THEN
       CHECK_FLAGS$=$04$
    FI
    PRINT (SYSGUI)'CHECKBOX'(99,450,5,100,25,"Grid Editable",CHECK_FLAGS$)
    
    REM Create grid using mnemonic. Give it a row and column header and set REM it to initally invisible
    PRINT (SYSGUI)'GRID'(GRID,20,30,544,230,$8856$,NUMROW,NUMCOL,NUMCOL,20,COLID,100,ROWID)
    NULL$=SENDMSG(SYSGUI,GRID,74,1,$$)
    ROWHEIGHT$=SENDMSG(SYSGUI,GRID,68,20,$$)
    
    PRINT (SYSGUI)'SHOW'(GRID)
    
    REM set columns to fit in grid
    TF$=SENDMSG(SYSGUI,GRID,29,NUMCOL,$$)
    
    REM set header titles using sendmsg 21 (set multiple cells)
    COLHEAD$="8 AM"+$0A$+"9 AM"+$0A$+"10 AM"+$0A$+"11 AM"+$0A$+"12 PM"+$0A$+"1 PM"+$0A$+"2 PM"+$0A$+"3 PM"+$0A$+"Rain"+$0A$
    TF$=SENDMSG(SYSGUI,COLID,21,NUMCOL,COLHEAD$)
    ROWHEAD$="Sunday"+$0A$+"Monday"+$0A$+"Tuesday"+$0A$+"Wednesday"+$0A$+"Thursday"+$0A$+"Friday"+$0A$+"Saturday"+$0A$
    TF$=SENDMSG(SYSGUI,ROWID,21,NUMROW,ROWHEAD$)
    
    REM set style of last column to checkbox using sendmsg 104
    TF$=SENDMSG(SYSGUI,GRID,104,8,$08$)
    
    REM Set a couple of the check boxes to checked using sendmsg 54
    DIM DRAW_DATA$:tmpl(sysgui,ind=1)
    
    REM load with default data
    DRAW_DATA$=SENDMSG(SYSGUI,GRID,20,0,$$)
    DRAW_DATA.ROW=1
    DRAW_DATA.COL=NUMCOL-1
    DRAW_DATA.STYLE=4
    TF$=SENDMSG(SYSGUI,GRID,54,0,DRAW_DATA$)
    DRAW_DATA.ROW=4
    TF$=SENDMSG(SYSGUI,GRID,54,0,DRAW_DATA$)
    
    REM use sendmsg 102 to set column colors
    FOR COL=0 TO NUMCOL-1
       IF COL<3 THEN
          RGB$=$00FF00$
       ELSE
          IF COL<6 THEN
             RGB$=$FFFF00$
          ELSE
             IF COL<8 THEN
                RGB$=$FF5500$
             ELSE
                RGB$=$9999FF$
             FI
          FI
       FI
       TF$=SENDMSG(SYSGUI,GRID,102,COL,RGB$)
    NEXT COL
    
    REM Set text in cells using sendmsg 109
    REM Set initial text to a generated number
    REM create the template used for sendmsg
    DIM CELL_DATA$:"col:u(2),row:u(4),buf:c(1*)"
    
    REM It is preferred to fill by rows rather than columns.
    REM Fill a full row before starting the next row
    FOR ROW=0 TO NUMROW-1
       FOR COL=0 TO NUMCOL-2
          CELL_DATA.BUF$=STR(50+ROW+COL)
          CELL_DATA.ROW=ROW
          CELL_DATA.COL=COL
          TF$=SENDMSG(SYSGUI,GRID,109,0,CELL_DATA$)
       NEXT COL
    NEXT ROW
    
    REM Set sendmsg 110 to not send notify event 22 if cells already contain data
    TF$=SENDMSG(SYSGUI,GRID,110,0,$$)
    
    GOSUB SET_EDITABLE
    
    REM Set drag and drop allowed
    TF$=SENDMSG(SYSGUI,GRID,33,1,$$)
    
    REM EVENT LOOP
    DIM EVENT$:TMPL(SYSGUI)
    DIM GENERIC$:NOTICETPL(0,0); REM ' Generic Notice Template
    EVENT=LEN(EVENT$)
    EOJ=0
    
    REPEAT
       READ RECORD (SYSGUI,SIZ=EVENT,ERR=EVENT_LOOP_END)EVENT$
    
       IF EVENT.CODE$="N" THEN
          GENERIC$=NOTICE(SYSGUI,EVENT.X%)
          DIM NOTICE$:NOTICETPL(GENERIC.OBJTYPE%,EVENT.FLAGS%)
          NOTICE$=GENERIC$
       FI
    
       IF EVENT.CODE$="X" THEN EOJ=1; BREAK
       IF EVENT.CODE$="c" THEN
          IF EDITABLE THEN
             EDITABLE=0
          ELSE
             EDITABLE=1
          FI
          GOSUB SET_EDITABLE
       FI
    
       IF EVENT.CODE$="N" AND EVENT.ID=GRID THEN
          REM Check for double click
          IF NOTICE.CODE=3 THEN
             GOSUB SHOW_CELL
          FI
       FI
    UNTIL EOJ
    
    EVENT_LOOP_END:
    PRINT (SYSGUI)'DESTROY'(0)
    RELEASE
    
    REM set editability of grid using sendmsg 108
    SET_EDITABLE:
    TF$=SENDMSG(SYSGUI,GRID,108,EDITABLE,$$)
    RETURN
    
    SHOW_CELL:
    IF EDITABLE=0 THEN
       REM Get text from cell and display in message box
       DIM GET_VAL$:"col:u(2),row:u(4)"
       GET_VAL$.ROW=NOTICE.ROW
       GET_VAL$.COL=NOTICE.COL
       if notice.col = numcol-1 then
          let text$=sendmsg(sysgui,grid,114,0,GET_VAL$)
          if dec(text$)= 0 then
             display$="No rain forecasted"
          else
             display$="Rain forecasted"
          fi
       else
          TEXT$=SENDMSG(SYSGUI,GRID,113,0,GET_VAL$)
          DISPLAY$="The temperature at this time will be "+$0A$+TEXT$+$0A$+"degrees Fahrenheit"
       fi
       REM Display a message box
       V=MSGBOX(DISPLAY$)
    FI
    RETURN
    END
  3. Create the basic grid control using the 'GRID ' mnemonic. Set the grid to initially invisible.

    REM Create grid using mnemonic. Give it a row and column header and set REM it to initally invisible
    PRINT (SYSGUI)'GRID'(GRID,20,30,544,230,$8856$,NUMROW,NUMCOL,NUMCOL,20,COLID,100,ROWID)
  4. Set attributes in the grid:

    REM set columns to fit in grid
    LET TF$=SENDMSG(SYSGUI,GRID,29,8,$$)
  5. Set header titles using SENDMSG() 21:

    REM set header titles using sendmsg 21 (set multiple cells)
    LET COLHEAD$="8 AM"+$0A$+"9 AM"+$0A$+"10 AM"+$0A$+"11 AM"+$0A$+"12 PM"+$0A$+"1 PM"+$0A$+"2 PM"+$0A$+"3 PM"+$0A$+"Rain"
    LET TF$=SENDMSG(SYSGUI,COLID,21,NUMCOL,COLHEAD$)
    LET ROWHEAD$= "Sunday"+$0A$+"Monday"+$0A$+"Tuesday"+$0A$+"Wednesday"+$0A$+"Thursday"+$0A$+"Friday"+$0A$+"Saturday"
    LET TF$=SENDMSG(SYSGUI,ROWID,21,NUMROW,ROWHEAD$)
  6. Set the style of the last column to checkbox using SENDMSG() 104:

    REM set style of last column to checkbox using sendmsg 104
    LET TF$=SENDMSG(SYSGUI,GRID,104,8,$08$)
  7. Set individual cells to checked:

    REM Set a couple of the check boxes to checked using sendmsg 54
    DIM DRAW_DATA$:"msg:u(4),wparm:u(4),lparm:i(4),col:u(4),row:u(4),
    textcolor:c(3),backcolor:c(3),alignment:u(1),style:i(4),imgidx:i(4),x:i(2),y:i(2),w:u(2),h:u(2),ptx:i(2),pty:i(2),buf:c(1*)"
    REM load with default data
    LET DRAW_DATA$=SENDMSG(SYSGUI,GRID,20,0,$$)
    LET DRAW_DATA.ROW=1
    LET DRAW_DATA.COL=NUMCOL-1
    LET DRAW_DATA.STYLE=4
    LET TF$=SENDMSG(SYSGUI,GRID,54,0,DRAW_DATA$)
    LET DRAW_DATA.ROW=4
    LET TF$=SENDMSG(SYSGUI,GRID,54,0,DRAW_DATA$)
  8. Set the colors of the columns using SENDMSG() 102:

    REM use sendmsg 102 to set column colors
    FOR COL=0 TO NUMCOL-1
        IF COL<3 THEN
            LET RGB$=$00FF00$
         ELSE
            IF COL<6 THEN
                LET RGB$=$FFFF00$
             ELSE
                IF COL<8 THEN
                    LET RGB$=$FF5500$
                 ELSE
                    LET RGB$=$9999FF$
                 FI
             FI
         FI
        LET TF$=SENDMSG(SYSGUI,GRID,102,COL,RGB$)
    NEXT COL
  9. Set the text in the grid cells using SENDMSG() 109:

    REM Set text in cells using sendmsg 109
    REM Set initial text to a generated number
    REM create the template used for sendmsg
    DIM CELL_DATA$:"col:u(2),row:u(4),buf:c(1*)"
    
    REM It is preferred to fill by rows rather than columns. Fill a full row before starting the next row
    FOR ROW=0 TO NUMROW-1
    FOR COL=0 TO NUMCOL-2
            LET CELL_DATA.BUF$=STR(50+ROW+COL)
            LET CELL_DATA.ROW=ROW
            LET CELL_DATA.COL=COL
            LET TF$=SENDMSG(SYSGUI,GRID,109,0,CELL_DATA$)
       NEXT COL
    NEXT ROW
  10. Set grid properties:

    REM Set sendmsg 110 to not send notify event 22 if cells already contain data
    LET TF$=SENDMSG(SYSGUI,GRID,110,0,$$)
    
    GOSUB SET_EDITABLE
    
    REM Set drag and drop allowed
    LET TF$=SENDMSG(SYSGUI,GRID,33,1,$$)
  11. Show the grid:

    PRINT (SYSGUI)'SHOW'(GRID)

    Grid appearance after shown:

    grid1.png

    Appearance of grid when cell edited:

    grid2.png

  12. Begin event loop. Event loop is only necessary for optional grid handling.

    REM EVENT LOOP
    DIM EVENT$:TMPL(SYSGUI)
    DIM GENERIC$:NOTICETPL(0,0); REM ' Generic Notice Template
    LET EVENT=LEN(EVENT$)
    LET EOJ=0
    
    REPEAT
        READ RECORD (SYSGUI,SIZ=EVENT,ERR=EVENT_LOOP_END)EVENT$
    
        IF EVENT.CODE$="N" THEN
            LET GENERIC$=NOTICE(SYSGUI,EVENT.X%)
            DIM NOTICE$:NOTICETPL(GENERIC.OBJTYPE%,EVENT.FLAGS%)
            LET NOTICE$=GENERIC$
         FI
        IF EVENT.CODE$="X" THEN LET EOJ=1; BREAK
  13. Editable check box was checked so toggle variable and reset editable in grid:

        IF EVENT.CODE$="c" THEN
            IF EDITABLE THEN
                LET EDITABLE=0
             ELSE
                LET EDITABLE=1
             FI
            GOSUB SET_EDITABLE
         FI
  14. Cell double clicked:

        IF EVENT.CODE$="N" AND EVENT.ID=GRID THEN
            REM Check for double click
            IF NOTICE.CODE=3 THEN
                GOSUB SHOW_CELL
            FI
         FI
    UNTIL EOJ
    
    EVENT_LOOP_END:
    PRINT (SYSGUI)'DESTROY'(0)
    RELEASE
  15. Set grid editability to EDITABLE:

    SET_EDITABLE:
    REM set editability of grid using sendmsg 108
    LET TF$=SENDMSG(SYSGUI,GRID,108,EDITABLE,$$)
    RETURN
  16. Use SENDMSG() 113 to retrieve value of clicked on cell and display it in a message box.

    SHOW_CELL:
    IF EDITABLE=0 THEN
        REM Get text from cell and display in message box
        DIM GET_VAL$:"col:u(2),row:u(4)"
        LET GET_VAL$.ROW=NOTICE.ROW
        LET GET_VAL$.COL=NOTICE.COL
        if notice.col = numcol-1 then
            let text$=sendmsg(sysgui,grid,114,0,GET_VAL$)   
            if dec(text$)= 0 then
                display$="No rain forcasted"
            else
                display$="Rain forcasted"
            fi
        else
            LET TEXT$=SENDMSG(SYSGUI,GRID,113,0,GET_VAL$)
            LET DISPLAY$="The temperature at this time will be " +$0A$ +TEXT$+ $0A$+"degrees Farenheit"
        fi
        REM Display a message box
        LET V=MSGBOX(DISPLAY$)
      
    FI
    RETURN
    END

    Appearance of grid after double-clicking on non-editable grid:

    grid3.png