TIPCORNER - Maintaining checkbox states
by Brad Moore

Home

Maintaining checkbox states
Tidbits from the community
Liberty Basic 4
New Alternate Forum
Using the Tsunami Database
Wizard Framework
Links to LB Sites
Update on 10th Anniversary Contest
Extracting Icons And Saving Them As Bitmaps
Applying Symbolic Logic
QuadClicks
Simple Math For Moving Objects
Event Driven Programming - Part 2
The Beginners Series - Part 1

 

A user asked the Liberty Basic group during the month of
December the following question:

"I've been trying to write some simple code in a modal dialog window,
but for some reason my checkboxes do not remain checked."

You are not maintaining the state of the checkboxes between successive displays. Each time you create your window (by invoking that section of code) it is a new thing. It does not KNOW what should be checked because you have not
told it.

Specifically you have not checked it.

What you must do is create a variable (or set of variables) that holds the state of each of the check boxes. Initialize these at the beginning of your code so that they are set to the default settings.

When you initialize your dialog window you will have to check (or uncheck) each box based on the given state of the variable.

if your variable is set to a "1" for instance then you will need to "set"
the checkbox using the following command:

print #handle.ext, "set"

However if the variable is set to a "0" (zero) then you know that the state is unchecked so issue a command to "reset" the checkbox. Use the following command:

print #handle.ext, "reset"

Here is a demo of the procedure:

'Demo of checkbox operation
'maintain status of checkboxes between seperate 
'events displaying the dialog window containing them.  
'by Brad Moore, 2002
'written for LB3.x
'released into the public domain.

    True = 1 : False = 0

    'set the default settings for the check buttons here
    cb1 = 0
    cb2 = 0
    cb3 = 0
    cb4 = 0
    cb5 = 0

[InitColors]
    ForegroundColor$ = "Black"
    BackgroundColor$ = "Buttonface"
    TexteditorColor$ = "White"
    TextboxColor$    = "White"
    ComboboxColor$   = "White"
    ListboxColor$    = "White"

[WindowSetup]
    NoMainWin
    WindowWidth = 295
    WindowHeight = 132
    UpperLeftX = Int((DisplayWidth-WindowWidth)/2)
    UpperLeftY = Int((DisplayHeight-WindowHeight)/2)

[ControlSetup]

Button      #main.reg, "Register my thoughts about LB",_
                       [reg],UL, 15, 15, 255, 30
Button      #main.quit, "I am all done...",_
                       [quit],UL, 15, 55, 255, 30

Open "Just for fun" For Dialog As #main

    Print #main, "trapclose [quit]"
    Print #main, "font ms_sans_serif 10"

[loop]
    Wait

[quit]
    Close #main
    End

[reg]
    GoTo [cb.showWin]

'-----------------------------------------------------------
'  Show the Sub-window: How Great is LB? 

[cb.showWin]
    WindowWidth = 237
    WindowHeight = 268
    UpperLeftX = Int((DisplayWidth-WindowWidth)/2)
    UpperLeftY = Int((DisplayHeight-WindowHeight)/2)

Statictext  #cbtest.st1, "Liberty Basic is:", _
                         15, 15, 169, 25
Button      #cbtest.ok, "OK",[ok],_
                         UL, 105, 200, 105, 25
Checkbox    #cbtest.cb1, "Easy to Use",[cb1.set],_
                         [cb1.reset], 35, 40, 165, 30
Checkbox    #cbtest.cb2, "Very affordable",[cb2.set],_
                         [cb2.reset], 35, 70, 165, 30
Checkbox    #cbtest.cb3, "Less Filling",[cb3.set],_
                         [cb3.reset], 35, 100, 165, 30
Checkbox    #cbtest.cb4, "Carls Great Achievment",[cb4.set],_
                         [cb4.reset], 35, 130, 175, 30
Checkbox    #cbtest.cb5, "Great Tasting",[cb5.set],_
                         [cb5.reset], 35, 160, 175, 30

Open "How Great is LB?" For Dialog As #cbtest

    Print #cbtest, "trapclose [cb.closewin]"
    Print #cbtest, "font ms_sans_serif 10"
    Print #cbtest.st1, "!font times_roman 14 bold"

'set each of the check boxes base on value of
'the variables used to track status of the
'checkboxes

    If cb1 = 1 Then
      Print #cbtest.cb1, "set"
    Else
      Print #cbtest.cb1, "reset"
    End If      

    If cb2 = 1 Then
      Print #cbtest.cb2, "set"
    Else
      Print #cbtest.cb2, "reset"
    End If      

    If cb3 = 1 Then
      Print #cbtest.cb3, "set"
    Else
      Print #cbtest.cb3, "reset"
    End If      

    If cb4 = 1 Then
      Print #cbtest.cb4, "set"
    Else
      Print #cbtest.cb4, "reset"
    End If      

    If cb5 = 1 Then
      Print #cbtest.cb5, "set"
    Else
      Print #cbtest.cb5, "reset"
    End If 

[cb.loop]
    Wait

[cb.closewin]
    Close #cbtest
    GoTo [loop]

[ok]
    Close #cbtest
    GoTo [loop]

'for each set or reset event - change the value of
'the variables tracking status of the checkboxes
[cb1.set]
    cb1 = 1
    GoTo [cb.loop]

[cb1.reset]
    cb1 = 0
    GoTo [cb.loop]

[cb2.set]
    cb2 = 1
    GoTo [cb.loop]

[cb2.reset]
    cb2 = 0
    GoTo [cb.loop]

[cb3.set]
    cb3 = 1
    GoTo [cb.loop]

[cb3.reset]
    cb3 = 0
    GoTo [cb.loop]

[cb4.set]
    cb4 = 1
    GoTo [cb.loop]

[cb4.reset]
    cb4 = 0
    GoTo [cb.loop]

[cb5.set]
    cb5 = 1
    GoTo [cb.loop]

[cb5.reset]
    cb5 = 0
    GoTo [cb.loop]



Home

Maintaining checkbox states
Tidbits from the community
Liberty Basic 4
New Alternate Forum
Using the Tsunami Database
Wizard Framework
Links to LB Sites
Update on 10th Anniversary Contest
Extracting Icons And Saving Them As Bitmaps
Applying Symbolic Logic
QuadClicks
Simple Math For Moving Objects
Event Driven Programming - Part 2
The Beginners Series - Part 1