In addition to fundamental GUI component interaction, an application should
dynamically enable and disable GUI components, to give some guidance to the
user, and prohibit (inter)actions which are inappropriate in the current state
of the application.



Task

Similar to the task GUI component interaction, write a program
that presents a form with three components to the user:

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


The field is initialized to zero.
The user may manually enter a new value into the field,
increment its value with the "increment" button,
or decrement the value with the "decrement" button.

The input field should be enabled only when its value is zero.
The "increment" button only as long as the field's value is less then 10:
When the value 10 is reached, the button should go into a disabled state.
Analogously, the "decrement" button should be enabled only as long as
the value is greater than zero.

Effectively, the user can now either increment up to 10, or down to zero.
Manually entering values outside that range is still legal,
but the buttons should reflect that and enable/disable accordingly.




 
#Include "windows.bi"
 
Dim As HWND Window_Main, Edit_Number, Button_Inc, Button_Dec
Dim As MSG msg
Dim As Integer n
Dim As String text
 
'Create a window with an input field and two buttons:
Window_Main = CreateWindow("#32770", "GUI Enabling/Disabling of Controls", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 250, 200, 0, 0, 0, 0)
Var Static_Number = CreateWindow("STATIC", "Value:", WS_VISIBLE Or WS_CHILD, 10, 10, 100, 20, Window_Main, 0, 0, 0)
Edit_Number = CreateWindow("EDIT", "0", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL Or ES_Number, 110, 10, 100, 20, Window_Main, 0, 0, 0)
Button_Inc = CreateWindow("BUTTON", "Increment", WS_VISIBLE Or WS_CHILD, 110, 40, 100, 20, Window_Main, 0, 0, 0)
Button_Dec = CreateWindow("BUTTON", "Decrement", WS_VISIBLE Or WS_CHILD, 110, 70, 100, 20, Window_Main, 0, 0, 0)
 
'Windows message loop:
While GetMessage(@msg, Window_Main, 0, 0)
  text = Space(GetWindowTextLength(Edit_Number) + 1)	'Buffer for the text
  GetWindowText(Edit_Number, text, Len(text))
  n = Val(text)
  If n <> 0 Then EnableWindow(Edit_Number, False) Else EnableWindow(Edit_Number, True)
  If n > 9 Then EnableWindow(Button_Inc, False) Else EnableWindow(Button_Inc, True)
  If n > 0 Then EnableWindow(Button_Dec, True) Else EnableWindow(Button_Dec, False) 
  TranslateMessage(@msg)
  DispatchMessage(@msg)
  Select Case msg.hwnd
    Case Button_Inc
      'Increment value:
      If msg.message = WM_LBUTTONDOWN Then SetWindowText(Edit_Number, Str(n + 1))
   Case Button_Dec
     'Decrement value:
     If msg.message = WM_LBUTTONDOWN Then SetWindowText(Edit_Number, Str(n - 1)) 
   Case Window_Main
    If msg.message = WM_COMMAND Then End
  End Select
Wend
 
End
 