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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm GUI component interaction step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

#Include "windows.bi"

Dim As HWND Window_Main, Edit_Number, Button_Inc, Button_Rnd
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 Component Interaction", 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_Rnd = CreateWindow("BUTTON", "Random", WS_VISIBLE Or WS_CHILD, 110, 70, 100, 20, Window_Main, 0, 0, 0)
													
'Windows message loop:
While GetMessage(@msg, Window_Main, 0, 0)
  TranslateMessage(@msg)
  DispatchMessage(@msg)
  Select Case msg.hwnd
    Case Button_Inc
      If msg.message = WM_LBUTTONDOWN Then
	'Increment value:
	text = Space(GetWindowTextLength(Edit_Number) + 1) 'Buffer for the text
	GetWindowText(Edit_Number, text, Len(text))
	n = Val(text)
	SetWindowText(Edit_Number, Str(n + 1))
      End If
    Case Button_Rnd
      If msg.message = WM_LBUTTONDOWN THEN
	'Random value (max. 100000):
	If MessageBox(0, "Set input field to random value?", "Please confirm", MB_ICONQUESTION Or MB_YESNO) = IDYES Then
	  n = 100000 * RND
	  SetWindowText(Edit_Number, Str(n))
	End If
      End If
    Case Window_Main
      If msg.message = WM_COMMAND Then End
  End Select
Wend

End

  

You may also check:How to resolve the algorithm Factorial primes step by step in the Sidef programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Scala programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the UnixPipes programming language
You may also check:How to resolve the algorithm Compare length of two strings step by step in the APL programming language