

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



hValueBox As ValueBox                                             'We need a ValueBox
 
Public Sub Form_Open()
Dim hButton As Button                                             'We need 2 Buttons
 
With Me                                                           'Set the Form's Properties..
  .height = 95                                                    'Set the Height
  .Width = 350                                                    'Set the Width
  .Arrangement = Arrange.Vertical                                 'Arrange items vertically
  .Padding = 5                                                    'Border area
  .Title = "GUI component interaction"                            'Title displayed on the Form 
End With
 
hValueBox = New ValueBox(Me)                                      'Add a ValueBox to the Form
 
With hValueBox                                                    'Set the ValueBox's Properties..
  .Expand = True                                                  'Expand the ValueBox
  .Value = 0                                                      'Set it's value to 0
End With
 
hButton = New Button(Me) As "ButtonInc"                           'Add a Button to the form as Event "ButtonInc"
 
With hButton                                                      'Set the Button's Properties..
  .Height = 28                                                    'Set the Height
  .Text = "&Increment"                                            'Add Text (The '&' adds a keyboard shortcut)
End With
 
hButton = New Button(Me) As "ButtonRand"                          'Add a Button to the form as Event "ButtonRand"
 
With hButton                                                      'Set the Button's Properties..
  .Height = 28                                                    'Set the Height
  .Text = "&Random"                                               'Add Text (The '&' adds a keyboard shortcut)
End With
 
End
 
Public Sub ButtonInc_Click()                                      'When the 'Increment' Button is clicked..
 
hValueBox.Value += 1                                              'Increase the Value in the ValueBox by 1
 
End
 
Public Sub ButtonRand_Click()                                     'When the 'Random' Button is clicked..
Dim siRand As Short                                               'To store random number
Dim byAnswer As Byte                                              'To store the answer to the MessageBox question
 
siRand = Rand(1, 32767)                                           'Create a 'Random' mnumber
 
byAnswer = Message.Question("Would you like to set the ValueBox to " & siRand & "?", "Yes", "No") ' Ask if the number is OK
If byAnswer = 1 Then                                              'If the user says 'Yes' then
  hValueBox.Value = siRand                                        'Display the 'Random' number in the ValueBox
Else                                                              'ELSE
  hValueBox.Value = 0                                             'Set the ValueBox Value to 0
End If
 
End