GUI component interaction: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
imported>Arakov
 
(13 intermediate revisions by 7 users not shown)
Line 13:
{{omit from|Lotus 123}}
{{omit from|Maxima}}
{{omit from|Minimal BASIC|It does not handle GUI}}
{{omit from|Nascom BASIC|It does not handle GUI}}
{{omit from|PARI/GP}}
{{omit from|PostScript}}
{{omit from|Retro}}
{{omit from|SQL PL|It does not handle GUI}}
{{omit from|Tiny BASIC|It does not handle GUI}}
 
{{omit from|Palo Alto Tiny BASIC|It does not handle GUI}}
 
Almost every application needs to communicate with the user in some way.
Line 51 ⟶ 54:
 
=={{header|1C}}==
<syntaxhighlight lang="text">
&НаСервере
Процедура ДобавитьЭлементы()
Line 110 ⟶ 113:
КонецПроцедуры
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
Line 116 ⟶ 119:
 
interaction.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
with Ada.Strings.Fixed;
with Gtk.Main;
Line 273 ⟶ 276:
 
Gtk.Main.Main;
end Interaction;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">GUI, add, Edit,Number w50 vUserInput gMakeSure, 0 ; Number Specifies Numbers-only, but other characters can still be pasted in,
; Making our own check necessary. (MakeSure)
 
Line 330 ⟶ 333:
ExitApp ; Makes sure the script exits when the window is closed,
; Otherwise the script is persistent because it contains
; a timer.</langsyntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
Requires BaCon version 4.0.1 or higher, using GTK3.
<langsyntaxhighlight lang="bacon">OPTION GUI TRUE
PRAGMA GUI gtk3
 
Line 369 ⟶ 373:
CALL GUIFN(gui, "confirm", hide)
ENDSELECT
WEND</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"WINLIB2"
INSTALL @lib$+"WINLIB5"
Line 402 ⟶ 406:
SYS "MessageBox", !form%, "Set to a random value?", "Confirm", MB_YESNO TO reply%
IF reply% = IDYES THEN SYS "SetDlgItemInt", !form%, 101, RND(10000), 1
ENDPROC</langsyntaxhighlight>
{{Out}}
<p>[[File:Guiintbbc.gif]]</p>
Line 411 ⟶ 415:
<pre>file main.c</pre>
 
<langsyntaxhighlight lang="c">#include <windows.h>
#include "resource.h"
 
Line 458 ⟶ 462:
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPInst, LPSTR cmdLn, int show ) {
return DialogBox( hInst, MAKEINTRESOURCE(IDD_DLG), NULL, DlgProc );
}</langsyntaxhighlight>
 
<pre>file resource.h</pre>
 
<langsyntaxhighlight lang="c">#define IDD_DLG 101
#define IDC_INPUT 1001
#define IDC_INCREMENT 1002
#define IDC_RANDOM 1003
#define IDC_QUIT 1004</langsyntaxhighlight>
 
<pre>file resource.rc</pre>
 
<langsyntaxhighlight lang="c">#include <windows.h>
#include "resource.h"
 
Line 483 ⟶ 487:
PUSHBUTTON "Random", IDC_RANDOM, 62, 25, 50, 14
PUSHBUTTON "Quit", IDC_QUIT, 117, 25, 30, 14
}</langsyntaxhighlight>
 
=={{header|C++}}==
with library Qt 4.4 , using (under Linux) first qmake -project and then qmake -o Makefile <projectfile>, then make
<pre>file interaction.h</pre>
<langsyntaxhighlight lang="cpp">#ifndef INTERACTION_H
#define INTERACTION_H
#include <QWidget>
Line 509 ⟶ 513:
void findRandomNumber( ) ;
} ;
#endif</langsyntaxhighlight>
<pre>file interaction.cpp</pre>
<langsyntaxhighlight lang="cpp">#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
Line 558 ⟶ 562:
break ;
}
}</langsyntaxhighlight>
<pre>file main.cpp </pre>
<langsyntaxhighlight lang="cpp">#include <QApplication>
#include "interaction.h"
 
Line 568 ⟶ 572:
theWidget.show( ) ;
return app.exec( ) ;
}</langsyntaxhighlight>
 
===C++11===
Line 575 ⟶ 579:
<pre>main.cpp</pre>
 
<langsyntaxhighlight lang="cpp">#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
Line 620 ⟶ 624:
 
return app.exec();
}</langsyntaxhighlight>
 
=={{header|C_sharp|C#}}==
C# 3.0 with Windows Forms; compile as csc -t:winexe Program.cs on MS.NET or as gmcs -t:winexe Program.cs on Mono.
<langsyntaxhighlight lang="csharp">using System;
using System.ComponentModel;
using System.Windows.Forms;
Line 697 ⟶ 701:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
{{libheader|LTK}}
<langsyntaxhighlight lang="lisp">
;; Using the LTK library...
 
Line 742 ⟶ 746:
(gui-test)
 
</syntaxhighlight>
</lang>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang Delphi="delphi">FILE: Unit1.pas</langsyntaxhighlight>
<syntaxhighlight lang="delphi">
<lang Delphi>
unit Unit1;
 
Line 799 ⟶ 803:
end;
 
end.</langsyntaxhighlight>
<langsyntaxhighlight Delphilang="delphi">FILE: Unit1.dfm (No manual interaction!!! Will automatically be generated/modified when editing the GUI)</langsyntaxhighlight>
<syntaxhighlight lang="delphi">
<lang Delphi>
object Form1: TForm1
Left = 1899
Line 845 ⟶ 849:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Echolisp}}==
<langsyntaxhighlight lang="scheme">
(require 'interface)
 
Line 889 ⟶ 893:
 
(panel)
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import forms;
import extensions;
public class MainWindow : SDIDialog
{
Button btmIncrement;
Button btmRandom;
Edit txtNumber;
constructor new()
<= super new()
{
btmIncrement := Button.new();
btmRandom := Button.new();
txtNumber := Edit.new();
self
.appendControl:(btmIncrement)
.appendControl:(btmRandom)
.appendControl:(txtNumber);
 
self.Caption := "Rosseta Code";
self.setRegion(100, 100, 160180, 120140);
txtNumber.setRegion(720, 7, 140, 25);
txtNumber.Caption := "0";
btmIncrement.setRegion(720, 35, 140, 25);
btmIncrement.Caption := "Increment";
btmIncrement.onClick := (args){ self.onButtonIncrementClick() };
btmRandom.setRegion(720, 65, 140, 25);
btmRandom.Caption := "Random";
btmRandom.onClick := (args){ self.onButtonRandomClick() };
}
private onButtonIncrementClick()
{
var number := txtNumber.Value.toInt();
number := number + 1;
self.changeTextBoxValue(number)
}
private onButtonRandomClick()
{
if(messageDialog.showQuestion("Inf", "Really reset to random value?"))
{
self.changeTextBoxValue(randomGenerator.evalnextInt(99999999))
}
}
private changeTextBoxValue(number)
{
txtNumber.Caption := number.toString()
}
}</langsyntaxhighlight>
 
=== Alternative version using xforms script ===
 
form layout:
<lang elena><Form X="100" Y="100" Width="160" Height="120" Caption="Rosseta Code">
<Edit ID="txtNumber" X="7" Y="7" Width="140" Height="25" Caption="0">
</Edit>
<Button ID="btmIncrement" X="7" Y="35" Width="140" Height="25" Caption="Increment" onClick="onButtonIncrementClick">
</Button>
<Button ID="btmRandom" X="7" Y="65" Width="140" Height="25" Caption="Random" onClick="onButtonRandomClick">
</Button>
</Form></lang>
 
main code:
<lang elena>import xforms;
import forms;
import extensions;
public class MainWindow
{
SDIForm form;
Button btmIncrement;
Button btmRandom;
Edit txtNumber;
constructor new()
{
form := xforms.executePath("main.xs", self);
btmIncrement := form.Controls.btmIncrement;
btmRandom := form.Controls.btmRandom;
txtNumber := form.Controls.txtNumber;
}
onButtonIncrementClick(sender)
{
var number := txtNumber.Value.toInt();
number := number + 1;
self.changeTextBoxValue(number)
}
onButtonRandomClick(sender)
{
if(messageDialog.showQuestion("Inf", "Really reset to random value?"))
{
self.changeTextBoxValue(randomGenerator.eval(99999999))
}
}
private changeTextBoxValue(number)
{
txtNumber.Caption := number.toString()
}
dispatch() => form;
}</lang>
 
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include GtkEngine.e -- see OpenEuphoria.org
 
Line 1,055 ⟶ 1,000:
end function
 
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// GUI component interaction. Nigel Galloway: June 13th., 2020
let n=new System.Windows.Forms.Form(Size=new System.Drawing.Size(250,150))
Line 1,071 ⟶ 1,016:
l.Click.AddHandler(new System.EventHandler(fun _ _->i.Text<-(string(rand.Next()))))
System.Windows.Forms.Application.Run(n)
</syntaxhighlight>
</lang>
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">
using fwt
using gfx
Line 1,140 ⟶ 1,085:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#Include "windows.bi"
 
Line 1,185 ⟶ 1,130:
 
End
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
begin enum 1
_valLabel
_valFld
_incBtn
_rndBtn
end enum
 
_confirmAlert = 1
 
void local fn BuildWindow
window _window, @"GUI Components", (0,0,214,100), NSWindowStyleMaskTitled
textlabel _valLabel, @"Value:", (20,61,42,16)
textfield _valFld,, @"0", (68,59,126,21)
ControlSetFormat( _valFld, @"0123456789", YES, 3, 0 )
button _incBtn,,, @"Increment", (13,13,95,32)
button _rndBtn,,, @"Random", (106,13,95,32)
WindowMakeFirstResponder( _window, _valFld )
end fn
 
void local fn DoAppEvent( ev as long )
select ( ev )
case _appDidFinishLaunching
random
fn BuildWindow
end select
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case _incBtn
long value = fn ControlIntegerValue( _valFld ) + 1
if ( value > 999 ) then value = 999
ControlSetStringValue( _valFld, fn StringWithFormat(@"%ld",value) )
case _rndBtn
long response = alert _confirmAlert,, @"Reset field", @"Do you want to reset the field to a random value?", @"OK;Cancel"
if ( response == NSAlertFirstButtonReturn )
ControlSetStringValue( _valFld, fn StringWithFormat(@"%ld",rnd(999)) )
end if
end select
end select
end fn
 
on appevent fn DoAppEvent
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
 
{{out}}
[[File:FB_GUIComponentInteraction.png]]
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">hValueBox As ValueBox 'We need a ValueBox
 
Public Sub Form_Open()
Line 1,243 ⟶ 1,247:
End If
 
End</langsyntaxhighlight>
 
=={{header|Go}}==
{{libheader|gotk3}}
{{trans|Vala}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,346 ⟶ 1,350:
window.ShowAll()
gtk.Main()
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight Haskelllang="haskell">import Graphics.UI.WX
import System.Random
 
Line 1,375 ⟶ 1,379:
answer <- confirmDialog frame "Random" "Generate a random number?" True
when answer $ getStdRandom (randomR (1,100)) >>= \num ->
set field [text := show (num :: Int)]</langsyntaxhighlight>
 
==Icon and {{header|Unicon}}==
Line 1,381 ⟶ 1,385:
This example uses the Unicon gui library, and so will not work in Icon.
 
<syntaxhighlight lang="unicon">
<lang Unicon>
import gui
$include "guih.icn"
Line 1,489 ⟶ 1,493:
w.show_modal ()
end
</syntaxhighlight>
</lang>
 
=={{header|J}}==
{{works with|J 8.x}}
<langsyntaxhighlight lang="j">INTERACT=: noun define
pc interact;
cc Value edit center;
Line 1,523 ⟶ 1,527:
wd 'set Value text ' , ": ?100
end.
)</langsyntaxhighlight>
{{works with|J 6.x}}
<langsyntaxhighlight lang="j">INTERACT=: 0 : 0
pc interact closeok;
xywh 6 6 48 12;cc Value edit;
Line 1,556 ⟶ 1,560:
wd 'set Value ' , ": ?100
end.
)</langsyntaxhighlight>
 
Note: I used an edit box for the value, and edit boxes do not get onChange events, so rejection of non-numeric values will be delayed until the use of a button (or pressing enter).
Line 1,562 ⟶ 1,566:
Example use:
 
<syntaxhighlight lang ="j">interact_run''</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,568 ⟶ 1,572:
{{works with|AWT}}
There are nice GUI editors in some IDEs (Netbeans has a notoriously good one), but this GUI was not built automatically, so it's a little easier to understand.
<langsyntaxhighlight lang="java">import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Line 1,660 ⟶ 1,664:
new Interact().setVisible(true);
}
}</langsyntaxhighlight>
{{works with|Java|8+}}
<langsyntaxhighlight lang="java5">import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
Line 1,690 ⟶ 1,694:
public void keyReleased(KeyEvent event);
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="java5">import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
Line 1,881 ⟶ 1,885:
;
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">#=
The task: For a minimal "application", write a program that
presents a form with three components to the user: A numeric input
Line 1,936 ⟶ 1,940:
 
while true sleep(1); end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
 
<langsyntaxhighlight lang="scala">import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
Line 1,991 ⟶ 1,995:
fun main(args : Array<String>) {
Interact().isVisible = true
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
Line 1,997 ⟶ 2,001:
See http://lambdaway.free.fr/lambdawalks/?view=interaction
 
<langsyntaxhighlight lang="scheme">
{input {@ id="input" type="text" value="0"}}
{input {@ type="button" value="increment"
Line 2,006 ⟶ 2,010:
document.getElementById('input').value =
Math.round(Math.random()*100);"}}
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
===Input Verification===
<langsyntaxhighlight lang="lb">nomainwin
textbox #demo.val, 20, 50, 90, 24
button #demo.inc, "Increment", [btnIncrement], UL, 20, 90, 90, 24
Line 2,065 ⟶ 2,069:
#demo.val nVal
end if
wait</langsyntaxhighlight>
 
===Impossible to type non-numeric characters===
<langsyntaxhighlight lang="lb">nomainwin
stylebits #demo.val, _ES_NUMBER, 0, 0, 0
textbox #demo.val, 20, 50, 90, 24
Line 2,094 ⟶ 2,098:
#demo.val nVal
end if
wait</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 2,100 ⟶ 2,104:
Version 2, addition a title for form (without title, default title if form variable name, form1).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Declare form1 form
Line 2,148 ⟶ 2,152:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
For this problem, you will need to open up Maple, go to the Components tab on the left, and insert a Text Area, and 2 Buttons. By right clicking each button, and clicking Component Properties, change one to Increase, and the other to Random. Then, click on 'Edit Content Changed Action". In the one labeled increase, type:
 
<syntaxhighlight lang="maple">
<lang Maple>
Increase();
</syntaxhighlight>
</lang>
 
In the one labeled Random, type:
 
<syntaxhighlight lang="maple">
<lang Maple>
Random();
</syntaxhighlight>
</lang>
 
Then, by clicking the 2 gears and opening up the start-up commands, enter this:
<syntaxhighlight lang="maple">
<lang Maple>
macro(SP=DocumentTools:-SetProperty, GP=DocumentTools:-GetProperty);
with(Maplets[Elements]):
Line 2,179 ⟶ 2,183:
end if;
end proc;
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="text">Manipulate[Null, {{value, 0}, InputField[Dynamic[value], Number] &},
Row@{Button["increment", value++],
Button["random",
Line 2,189 ⟶ 2,193:
Row@{Button["Yes", DialogReturn[True]],
Button["No", DialogReturn[False]]}}],
value = RandomInteger@10000], Method -> "Queued"]}]</langsyntaxhighlight>
 
=={{header|Nim}}==
===Gtk2===
{{libheader|Gtk2}}
<langsyntaxhighlight lang="nim">import
gtk2, glib2, strutils, random
Line 2,253 ⟶ 2,257:
win.show_all()
main()</langsyntaxhighlight>
 
===Gtk3 (gintro)===
{{libheader|gintro}}
This is a translation of the Gtk2 version to Gtk3 using the higher level "gintro" bindings. The higher level is not really obvious in this small example, but, for instance, we are able to transmit to signal handlers a pure Nim “Context” object (managed by the GC). With the Gtk2 bindings, it would require to use unsafe addresses.
<langsyntaxhighlight Nimlang="nim">import random, strutils
import gintro/[glib, gobject, gtk, gio]
 
Line 2,352 ⟶ 2,356:
let app = newApplication(Application, "Rosetta.ComponentInteraction")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
===IUP===
{{libheader|IUP}}
<langsyntaxhighlight lang="nim">import
iup, strutils, math
 
Line 2,418 ⟶ 2,422:
discard dlg.show()
discard mainloop()
iup.close()</langsyntaxhighlight>
 
=={{header|Oz}}==
Using Mozart's standard GUI library, building a small desktop application:
<langsyntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
 
Line 2,463 ⟶ 2,467:
end
in
{Main}</langsyntaxhighlight>
 
As a web application, using the "Roads" web programming library. Connect your browser to http://localhost:8080/start after starting the program.
<langsyntaxhighlight lang="oz">declare
[Roads] = {Module.link ['x-ozlib://wmeyer/roads/Roads.ozf']}
 
Line 2,521 ⟶ 2,525:
in
{Roads.registerFunction 'start' Start}
{Roads.run}</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 2,550 ⟶ 2,554:
)->pack( -side => 'right' );
 
MainLoop;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
{{libheader|Phix/pGUI}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 2,590 ⟶ 2,594:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
The standard PicoLisp GUI is HTTP based. Connect your browser to
http://localhost:8080 after starting the following script.
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(load "@ext.l" "@lib/http.l" "@lib/xhtml.l" "@lib/form.l")
Line 2,612 ⟶ 2,616:
 
(server 8080 "!start")
(wait)</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">[xml]$XML = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Rosetta Code" WindowStartupLocation = "CenterScreen" Height="100" Width="210" ResizeMode="NoResize">
Line 2,639 ⟶ 2,643:
$buttonIncrement.add_click({++[Int]$textBoxValue.Text})
 
$XMLForm.ShowDialog() | Out-Null</langsyntaxhighlight>
 
=={{header|Prolog}}==
Works with SWI-Prolog and XPCE.
<langsyntaxhighlight Prologlang="prolog">dialog('GUI_Interaction',
[ object :=
GUI_Interaction,
Line 2,724 ⟶ 2,728:
( send(@display, inform, 'Please type a number !'),
send(Input,clear))).
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Enumeration
#StringGadget
#Increment
Line 2,754 ⟶ 2,758:
Until Event=#PB_Event_CloseWindow
CloseWindow(0)
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,761 ⟶ 2,765:
{{libheader|Tkinter}}
{{works with|Python|2.7}}
<langsyntaxhighlight lang="python">
import random, tkMessageBox
from Tkinter import *
Line 2,785 ⟶ 2,789:
b2 = Button(text="Random", command=rand, **options)
b2.grid(column=2, row=0, **options)
mainloop()</langsyntaxhighlight>
 
===Python3 - no classes===
{{libheader|Tkinter}}
{{works with|Python|3.7}}
<langsyntaxhighlight lang="python">
import random, tkinter.messagebox
from tkinter import *
Line 2,819 ⟶ 2,823:
b2.grid(column=2, row=0, **options)
 
mainloop()</langsyntaxhighlight>
 
===Python2 - With classes===
{{libheader|Tkinter}}
<langsyntaxhighlight lang="python">import random
from Tkinter import *
import tkMessageBox
Line 2,875 ⟶ 2,879:
app.mainloop()
except KeyboardInterrupt:
root.destroy()</langsyntaxhighlight>
 
[[File:GUI_component_interaction_Python.png]]
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
library(gWidgets)
options(guiToolkit="RGtk2") ## using gWidgtsRGtk2
Line 2,911 ⟶ 2,915:
svalue(e) <- sample(1:1000, 1)
})
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket/gui
Line 2,940 ⟶ 2,944:
 
(send frame show #t)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,947 ⟶ 2,951:
{{libheader|GTK}}
 
<syntaxhighlight lang="raku" perl6line>use GTK::Simple;
use GTK::Simple::App;
 
Line 2,980 ⟶ 2,984:
}
 
$app.run;</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
Load "guilib.ring"
 
Line 3,015 ⟶ 3,019:
num = string(number(num)+1)
lineedit1.settext(num)
</syntaxhighlight>
</lang>
 
Output:
Line 3,023 ⟶ 3,027:
=={{header|Ruby}}==
{{libheader|Shoes}}
<langsyntaxhighlight lang="ruby">Shoes.app(title: "GUI component interaction") do
stack do
textbox = edit_line
Line 3,041 ⟶ 3,045:
end
end
end</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">dim dd$(5) ' drop down box
for i = 1 to 5
dd$(i) = "Drop ";i
Line 3,086 ⟶ 3,090:
[exit]
print "Bye"
end</langsyntaxhighlight>
[[File:GuiComponentRunBasic.png]]
 
== {{Header|Rust}} ==
{{libheader|egui}}
<syntaxhighlight lang="rust">
use eframe::egui;
use rand::Rng;
 
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(214.0, 100.0)),
..Default::default()
};
 
// Our application state:
let mut value = "0".to_owned();
 
eframe::run_simple_native("GUI component interaction", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
let name_label = ui.label("Value: ");
ui.text_edit_singleline(&mut value)
.labelled_by(name_label.id);
});
ui.horizontal(|ui| {
if ui.button("Increment").clicked() {
if let Ok(v) = value.parse::<usize>() {
value = (v + 1).to_string()
}
}
if ui.button("Random").clicked() {
value = (rand::thread_rng().gen_range(1..=10000)).to_string();
}
});
});
})
}</syntaxhighlight>
[[File:Rust GUI component interaction.png|none|thumb]]
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import scala.swing._
import scala.swing.Swing._
import scala.swing.event._
Line 3,134 ⟶ 3,175:
centerOnScreen()
}
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">|top input vh incButton rndButton|
 
vh := ValueHolder with:0.
Line 3,155 ⟶ 3,196:
rndButton action:[ vh value: Random nextInteger ].
 
top open</langsyntaxhighlight>
{{Out}}
[[File:Guiintsmalltalkx.png]]
Line 3,161 ⟶ 3,202:
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
###--- Our data Model! ---###
Line 3,195 ⟶ 3,236:
set field [expr {int(rand() * 5000)}]
}
}</langsyntaxhighlight>
 
=={{header|Vala}}==
{{libheader|Gtk+-3.0}}
 
<langsyntaxhighlight lang="vala">bool validate_input(Gtk.Window window, string str){
int64 val;
bool ret = int64.try_parse(str,out val);;
Line 3,281 ⟶ 3,322:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Visual Basic}}==
Line 3,289 ⟶ 3,330:
Note that there are other methods of validating the entered value. The method used is dependant on the program's requirements.
 
<langsyntaxhighlight lang="vb">VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
Line 3,350 ⟶ 3,391:
KeyAscii = 0
End Select
End Sub</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
Graphical
 
Notes:
 
1) "v install ui" to get, also to locally check source and examples.
 
2) For alternative UI toolkits, check github.com/vlang/awesome-v (Awesome V).
<syntaxhighlight lang="Zig">
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()
}
</syntaxhighlight>
 
=={{header|Web 68}}==
Web 68 uses an include file to use the Xforms library.
<langsyntaxhighlight lang="web68">@1Rosetta code program.
@aPROGRAM guicomponent CONTEXT VOID USE standard
BEGIN
Line 3,483 ⟶ 3,608:
macro fl show form;
 
@ The end.</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "input" for Mouse, Keyboard
import "dome" for Window
import "random" for Random
import "./polygon" for Polygon
 
var Rand = Random.new()
 
class Button {
construct new(x, y, w, h, legend, c, oc, lc) {
var vertices = [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]
_rect = Polygon.quick(vertices)
_x = x
_y = y
_w = w
_h = h
_legend = legend
_c = c
_oc = oc
_lc = lc
}
 
draw() {
_rect.drawfill(_c)
_rect.draw(_oc)
var l = Canvas.getPrintArea(_legend)
var lx = ((_w - l.x)/2).truncate
lx = (lx > 0) ? _x + lx : _x + 1
var ly = ((_h - l.y)/2).truncate
ly = (ly > 0) ? _y + ly : _y + 1
Canvas.print(_legend, lx, ly, _lc)
}
 
justClicked { Mouse["left"].justPressed && _rect.contains(Mouse.position.x, Mouse.position.y) }
}
 
class TextBox {
construct new(x, y, w, h, label, c, oc, lc) {
var vertices = [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]
_rect = Polygon.quick(vertices)
_x = x
_y = y
_w = w
_h = h
_label = label
_c = c
_oc = oc
_lc = lc
_text = ""
}
 
text { _text }
text=(t) { _text = t }
 
draw() {
_rect.drawfill(_c)
_rect.draw(_oc)
var l = Canvas.getPrintArea(_label).x
var lx = _x - l - 7
if (lx < 1) {
lx = 1
_label = _label[0..._x]
}
Canvas.print(_label, lx, _y, _lc)
Canvas.getPrintArea(_label).x
Canvas.print(_text, _x + 3, _y + 1, Color.black)
}
}
 
class GUIComponentInteraction {
construct new() {
Window.title = "GUI component interaction"
_btnIncrement = Button.new(60, 40, 80, 80, "Increment", Color.red, Color.blue, Color.white)
_btnRandom = Button.new(180, 40, 80, 80, "Random", Color.green, Color.blue, Color.white)
_txtValue = TextBox.new(140, 160, 80, 8, "Value", Color.white, Color.blue, Color.white)
_txtValue.text = "0"
Keyboard.handleText = true
_waiting = false
}
 
init() {
drawControls()
}
 
update() {
if (_waiting) {
if (Keyboard["Y"].justPressed) {
var rn = Rand.int(1000) // max 999 say
_txtValue.text = rn.toString
_waiting = false
} else if (Keyboard["N"].justPressed) {
_waiting = false
}
} else if (_btnIncrement.justClicked) {
var number = Num.fromString(_txtValue.text) + 1
_txtValue.text = number.toString
} else if (_btnRandom.justClicked) {
Canvas.print("Reset to a random number y/n?", 60, 200, Color.white)
_waiting = true
} else if ("0123456789".any { |d| Keyboard[d].justPressed }) {
if (_txtValue.text != "0") {
_txtValue.text = _txtValue.text + Keyboard.text
} else {
_txtValue.text = Keyboard.text
}
}
}
 
draw(alpha) {
if (!_waiting) drawControls()
}
 
drawControls() {
Canvas.cls()
_btnIncrement.draw()
_btnRandom.draw()
_txtValue.draw()
}
}
 
var Game = GUIComponentInteraction.new()</syntaxhighlight>
Anonymous user