New to handcode, refresh window

I believe that my lack of knowledge over hand coded .clw is holding me back. My code compiles but the buttons do not refresh the screen.

This is a basic dice rolling app. When I click the button the window does not display the results. What have I missed:

PROGRAM

 MAP
DiceRollerProcedure PROCEDURE
 END

CODE
 DiceRollerProcedure
  RETURN

!-----------------------------------
DiceRollerProcedure PROCEDURE

RollResult LONG

DiceRollerWindow WINDOW('Dice Roller'),AT(,,200,150),GRAY,AUTO
    BUTTON('Roll D6'),AT(20,20,60,20),USE(?BtnD6)
    BUTTON('Roll D20'),AT(100,20,60,20),USE(?BtnD20)
    STRING('Result:'),AT(20,70,40,15)
    STRING(@n3),AT(70,70,30,15),USE(?RollResult)
END

CODE
  OPEN(DiceRollerWindow)
  ACCEPT
    CASE FIELD()
    OF ?BtnD6
      RollResult = RANDOM(1,6)
        DISPLAY(?RollResult)        
    OF ?BtnD20
      RollResult = RANDOM(1,20)
      DISPLAY(?RollResult)
    END
  END
  CLOSE(DiceRollerWindow)
  RETURN

I would make a few changes.

  1. You’re STRING for RoleResult needs to have USE(RollResults) not ?RollResult.
  2. You should check for the Event() for the button push. Right now your code will execute for any event for ?btnD6 or ?btnD20
!-----------------------------------
DiceRollerProcedure PROCEDURE

RollResult LONG

DiceRollerWindow WINDOW('Dice Roller'),AT(,,200,150),GRAY,AUTO
    BUTTON('Roll D6'),AT(20,20,60,20),USE(?BtnD6)
    BUTTON('Roll D20'),AT(100,20,60,20),USE(?BtnD20)
    STRING('Result:'),AT(20,70,40,15)
    STRING(@n3),AT(70,70,30,15),USE(RollResult)
END

CODE
  OPEN(DiceRollerWindow)
  ACCEPT
    CASE FIELD()
    OF ?BtnD6
       CASE EVENT()
       OF Event:Accepted
          RollResult = RANDOM(1,6)
          DISPLAY(?RollResult)        
       END
    OF ?BtnD20
       CASE EVENT()
       OF Event:Accepted
          RollResult = RANDOM(1,20)
          DISPLAY(?RollResult)
      END
    END
  END
  CLOSE(DiceRollerWindow)
  RETURN
1 Like

Instead for less code you can check for Case ACCEPTED(). That’s what I normally do in a hand coded program. Then above or below that I’ll check for Case FIELD() and special events like Alert keys or List events.

  ACCEPT
    CASE ACCEPTED()    !was FIELD()
    OF ?BtnD6
          RollResult = RANDOM(1,6)
          DISPLAY(?RollResult)        
      
    OF ?BtnD20
          RollResult = RANDOM(1,20)
          DISPLAY(?RollResult)
     
    END
  END

You can add the AUTO attribute you the Window, then you do not need to code Display(). Sometimes the Display is still needed.

Thanks so much for the help. Grateful to both of you.

For writing quick hand code it helps to start with a Scratch Program that has a common framework. I have a few for Windows and Reports

Here’s the “lite” one:

    PROGRAM  !Scratch Lite program by Carl Barnes - Version 9/1/2022 - Download https://git.io/JtPKE
    INCLUDE('TplEqu.CLW')
    INCLUDE('KeyCodes.CLW')
    MAP
Main        PROCEDURE()
DB          PROCEDURE(STRING DebugMessage)
      MODULE('api')
        OutputDebugString(*CSTRING cMsg),PASCAL,DLL(1),RAW,NAME('OutputDebugStringA')
      END
    END

    CODE
    MAIN()
    RETURN
!----------------------------
Main   PROCEDURE

Window WINDOW('Scratch '),AT(,,400,200),CENTER,GRAY,IMM,SYSTEM,STATUS,FONT('Segoe UI',9),RESIZE
        BUTTON('Button1'),AT(155,180),USE(?Button1)
        BUTTON('Button2'),AT(206,180),USE(?Button2)
    END

    CODE
    OPEN(WINDOW)
    0{PROP:text}=clip(0{PROP:text}) &' - Library ' & system{PROP:LibVersion,2} &'.'& system{PROP:LibVersion,3}
    ACCEPT
        CASE EVENT()
        OF EVENT:OpenWindow 
        OF EVENT:CloseWindow
        OF EVENT:PreAlertKey
        OF EVENT:AlertKey
        OF EVENT:Timer
        END
        CASE ACCEPTED()
!        OF ?Button1
!        OF ?Button2
        END
        CASE FIELD()
!        OF ?Button1
!        OF ?Button2
        END
    END
    CLOSE(WINDOW)
!===============================
DB   PROCEDURE(STRING xMessage)
Prfx EQUATE('Scratch: ')
sz   CSTRING(SIZE(Prfx)+SIZE(xMessage)+1),AUTO
  CODE 
  sz  = Prfx & CLIP(xMessage)
  OutputDebugString( sz )
1 Like