How to resolve the algorithm GUI component interaction step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm GUI component interaction step by step in the FutureBasic programming language

Table of Contents

Problem Statement

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:

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

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).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm GUI component interaction step by step in the FutureBasic programming language

Source code in the futurebasic programming language

_window = 1
begin enum 1
  _valLabel
  _valFld
  _incBtn
  _rndBtn
end enum

_confirmAlert = 1

void local fn BuildWindow
  window _window, @"GUI Components", (0,0,214,100), NSWindowStyleMaskTitled
  
  textlabel _valLabel, @"Value:", (20,61,42,16)
  textfield _valFld,, @"0", (68,59,126,21)
  ControlSetFormat( _valFld, @"0123456789", YES, 3, 0 )
  
  button _incBtn,,, @"Increment", (13,13,95,32)
  button _rndBtn,,, @"Random", (106,13,95,32)
  
  WindowMakeFirstResponder( _window, _valFld )
end fn

void local fn DoAppEvent( ev as long )
  select ( ev )
    case _appDidFinishLaunching
      random
      fn BuildWindow
  end select
end fn

void local fn DoDialog( ev as long, tag as long )
  select ( ev )
    case _btnClick
      select ( tag )
        case _incBtn
          long value = fn ControlIntegerValue( _valFld ) + 1
          if ( value > 999 ) then value = 999
          ControlSetStringValue( _valFld, fn StringWithFormat(@"%ld",value) )
        case _rndBtn
          long response = alert _confirmAlert,, @"Reset field", @"Do you want to reset the field to a random value?", @"OK;Cancel"
          if ( response == NSAlertFirstButtonReturn )
            ControlSetStringValue( _valFld, fn StringWithFormat(@"%ld",rnd(999)) )
          end if
      end select
  end select
end fn

on appevent fn DoAppEvent
on dialog fn DoDialog

HandleEvents

  

You may also check:How to resolve the algorithm Palindrome detection step by step in the Elm programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Julia programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Java programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Icon and Unicon programming language