

Almost every application needs to communicate with the user in some way.
Therefore, a substantial part of the code deals with the interaction
of program logic with GUI components.

Typically, the following is needed:

 put values into input fields under program control
 read and check input from the user
 pop up dialogs to query the user for further information


Task

For a minimal "application", write a program that presents a form with three components to the user:

 a numeric input field ("Value") 
 a button ("increment")
 a button ("random")


The field is initialized to zero.

The user may manually enter a new value into the field,
or increment its value with the "increment" button.

Entering a non-numeric value should be either impossible,
or issue an error message.

Pressing the "random" button presents a confirmation dialog,
and resets the field's value to a random value if the answer is "Yes".

(This task may be regarded as an extension of the task Simple windowed application).



nomainwin
    textbox #demo.val, 20, 50, 90, 24
    button #demo.inc, "Increment", [btnIncrement], UL, 20, 90, 90, 24
    button #demo.rnd, "Random", [btnRandom], UL, 20, 120, 90, 24
    open "Rosetta Task: GUI component interaction" for window as #demo
    #demo "trapclose [quit]"
    validNum$ = "0123456789."
    #demo.val 0
wait
 
[quit]
    close #demo
end
 
[btnIncrement]
    #demo.val "!contents? nVal$"
    nVal$ = trim$(nVal$)
    if left$(nVal$, 1) = "-" then
        neg = 1
        nVal$ = mid$(nVal$, 2)
    else
        neg = 0
    end if
    validNum = 1
    nDecs = 0
    for i = 1 to len(nVal$)
        if instr(validNum$, mid$(nVal$, i, 1)) = 0 then
            validNum = 0
        end if
        if mid$(nVal$, i, 1) = "." then
            nDecs = nDecs + 1
        end if
    next i
    if nDecs > 1 then
        validNum = 0
    end if
    if neg = 1 then
        nVal$ = "-";nVal$
    end if
    if validNum = 0 then
        notice nVal$;" does not appear to be a valid number.  " + _
            "(Commas are not allowed.)"
    else
        nVal = val(nVal$)
        nVal = nVal + 1
    end if
    #demo.val nVal
wait
 
[btnRandom]
    confirm "Reset value to random number";yn$
    if yn$ = "yes" then
        nVal = int(rnd(1) * 100) + 1
        #demo.val nVal
    end if
wait