How to resolve the algorithm GUI component interaction step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

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

Source code in the v programming language

import ui
import gx
import rand

const (
	win_width  = 400
	win_height = 40
)

[heap]
struct App {
	mut:
	window &ui.Window = unsafe {nil}
	counter string = "0"
}

fn main() {
	mut app := &App{}
	app.window = ui.window(
		width: win_width
		height: win_height
		title: "Counter"
		mode: .resizable
		layout: ui.row(
			spacing: 5
			margin_: 10
			widths: ui.stretch
			heights: ui.stretch
			children: [
				ui.textbox(
					max_len: 20
					read_only: false
//					is_numeric: true // Can enforce only number input
					text: &app.counter
				),
				ui.button(
					text: "increment"
					bg_color: gx.light_gray
					radius: 5
					border_color: gx.gray
					on_click: app.btn_click_inc
				),
				ui.button(
					text: "random"
					bg_color: gx.light_gray
					radius: 5
					border_color: gx.gray
					on_click: app.btn_click_ran
				),
			]
		)
	)
	ui.run(app.window)
}

fn (mut app App) btn_click_inc(mut btn ui.Button) {
	for value in app.counter {
		if value.is_digit() == false {
			ui.message_box("Only numbers allowed!") 
			return
		}
	}
	if app.counter.int() < 0 || app.counter.int() > 100 {
		ui.message_box("Only numbers between 0 to 100 accepted!\nResetting to 1.")
		app.counter = "0"
	}
	app.counter = (app.counter.int() + 1).str()
}

fn (mut app App) btn_click_ran(mut btn ui.Button) {
	ui.message_box("Will jump to random number between 1 and 100.")
	app.counter = rand.int_in_range(1, 100) or {0}.str()	
}


  

You may also check:How to resolve the algorithm Jump anywhere step by step in the Lua programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the BASIC programming language
You may also check:How to resolve the algorithm Word frequency step by step in the APL programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Picat programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the F# programming language