How to resolve the algorithm GUI enabling/disabling of controls step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm GUI enabling/disabling of controls step by step in the V (Vlang) programming language

Table of Contents

Problem Statement

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.

Similar to the task GUI component interaction, 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, 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm GUI enabling/disabling of controls step by step in the V (Vlang) programming language

Source code in the v programming language

import ui
import gx

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
					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: "decrement"
					bg_color: gx.light_gray
					radius: 5
					border_color: gx.gray
					on_click: app.btn_click_dec
				),
			]
		)
	)
	ui.run(app.window)
}

fn (mut app App) btn_click_inc(mut btn ui.Button) {
	if app.counter.int() > 10 {
		btn.disabled = true
		return
	}
	app.counter = (app.counter.int() + 1).str()
}

fn (mut app App) btn_click_dec(mut btn ui.Button) {
	if app.counter.int() < 0 {
		btn.disabled = true 
		return
	}
	app.counter = (app.counter.int() - 1).str()	
}


  

You may also check:How to resolve the algorithm Averages/Median step by step in the Excel programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Golo programming language
You may also check:How to resolve the algorithm Factorions step by step in the Julia programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Guess the number step by step in the ERRE programming language