Page 1 of 1

Modifying GUI text - what could I´ve done better?

Posted: 22 Oct 2019, 14:48
by hendrikm
Hi,
I run some unsupervised workstations. At starttimme, alot of programs are loaded and setiings are made. I want to inform the user about the status of the startup process...

I made quite some progress today. Worked first time with arrays (pat on the back) and modyfing textcolor :bravo:
What killed me:
-> position :=++ somehow didn´t work, had to use position := position+1
->Gui, Add, Text, x30 y(%position%*30) w160 h25, %inhalt% didn´t work but Gui, Add, Text, % "x30" "y" (position*30) "w160" "h25", %inhalt% does

This is just a little script that displays some items and if you press the buttons, the red items will become green from top to bottom one at at time, just like a list of chores...

In future I´ll probably just call the function after a certain program is loaded, therefore I chose to use a separate function to display the GUI.

But what could I´ve done different, what could I improve?
I´m not happy about destroying and recreating the GUI as it creates some flicker, but found no other way...

Code: Select all

#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Persistent
SetTitleMatchMode,2
SetTitleMatchMode, slow
DetectHiddenWindows, On

arrayitem := ["Apples", "Oranges", "Tomatoes", "Bananas", "Melons"]
global position :=0
global fertig :=-1
;MsgBox % ObjMaxIndex(arrayitem) " = " arrayitem.MaxIndex()
;MsgBox % ObjRawGet(arrayitem, position)

gosub, aktualisieren
return

aktualisieren:
Gui, Destroy
fertig := fertig+1
if (fertig>arrayitem.MaxIndex()) 
	{
		gosub, abbruch
	}
position :=0
;MsgBox fertig%fertig%   position%position%
Gosub,showlist
return


abbruch:
GuiClose:
ExitApp
return

showlist:
Gui, Show, w1200 h800, WATCHDOG
Loop
	{
	position := position+1
	inhalt := objRawGet(arrayitem, position)
	If (position>fertig)
	{
		Gui, Font, S15 cRed Bold, Verdana
		}
	else 
	{
		Gui, Font, S15 cGreen Bold, Verdana
		}
	Gui, Add, Text, % "x30" "y" (position*30) "w160" "h25",  %inhalt%

	;SoundBeep
	;MsgBox %inhalt%  %position% wert
	;sleep 1000
	}
Until position > arrayitem.MaxIndex()
Gui, Add, Button, x112 y579 w250 h70 gaktualisieren , Aktualisieren 
Gui, Add, Button, x472 y579 w350 h70 gabbruch, Abbruch
return
[Mod edit: [code][/code] tags added]


Thanks, Hendrik

Re: Modifying GUI text - what could I´ve done better?

Posted: 22 Oct 2019, 16:26
by Hellbent
Hi hendrikm.

Please be sure to use the code tags when you share your code. As is I'm getting notepad flashbacks.

Is this along the lines of what you're trying to do?

Code: Select all

#SingleInstance,Force
arrayitem := ["Apples", "Oranges", "Tomatoes", "Bananas", "Melons"]
CurrentIndex:=0
Gui,1:+AlwaysOnTop -Theme
Gui,1:Color,222222,333333
Gui,1:Font,cRed s12,Segoe UI
for,k,v in arrayitem
   Gui,1:Add,Text,% "vItem" A_Index,% v
Gui,1:Font,s10,Segoe UI
Gui,1:Add,Button,xm y+30 w150 r1 gUpdate,Update
Gui,1:Add,Button,xm w150 r1 gGuiClose,Exit
Gui,1:Font,cGreen s12,Segoe UI
Gui,1:Show,,
return
GuiClose:
GuiContextMenu:
*ESC::
   ExitApp

Update:
   if(++CurrentIndex<=arrayitem.Length())
      GuiControl,1:Font,Item%CurrentIndex%
   else
      ExitApp
   return

Re: Modifying GUI text - what could I´ve done better?

Posted: 22 Oct 2019, 16:37
by flyingDman
Obviously @Hellbent beat me to it but here is another version (which is actually remarkably similar; I did not change your layout)

Code: Select all

arrayitem := ["Apples", "Oranges", "Tomatoes", "Bananas", "Melons"]
fertig := 0

Gui, Font, S15 cred Bold, Verdana
Loop, % arrayitem.length()
	Gui, Add, Text, x30 yp+30 w160 h25 vItem%a_index%, % arrayitem[a_index]
Gui, Add, Button, x112 y579 w250 h70 gaktualisieren , Aktualisieren
Gui, Add, Button, x472 y579 w350 h70 gabbruch, Abbruch
Gui, Show, w1200 h800, WATCHDOG
return

aktualisieren:
++fertig
if (fertig > arrayitem.MaxIndex())
	gosub, abbruch
Gui, Font, S15 cGreen Bold, Verdana
GuiControl, Font, Item%fertig%
return

abbruch:
GuiClose:
ExitApp
return

Re: Modifying GUI text - what could I´ve done better?

Posted: 22 Oct 2019, 17:30
by Hellbent
In case having your gui in its own subroutine is a important factor to you, I've slightly modified my example to have the gui outside the auto-execute section.

@flyingDman's version can also easily be edited to do the same. Just need to move it to its own routine and add either

Gui,Destroy or Gui,New to the top of the routine, ( fertig := 0 should also be moved to reset your counting variable every time the gui is created)

Code: Select all

#SingleInstance,Force
arrayitem := ["Apples", "Oranges", "Tomatoes", "Bananas", "Melons"]
gosub,CreateGui
return
GuiClose:
   ExitApp

CreateGui:
   CurrentIndex:=0
   Gui,1:New,+AlwaysOnTop -Theme
   Gui,1:Color,222222
   Gui,1:Font,cRed s12,Segoe UI
   for,k,v in arrayitem
      Gui,1:Add,Text,hwndItem%k%,% v
   Gui,1:Font,s10
   Gui,1:Add,Button,xm y+30 w150 r1 gUpdate,Update
   Gui,1:Add,Button,xm w150 r1 gGuiClose,Exit
   Gui,1:Font,cGreen s12
   Gui,1:Show,,
   return

Update:
   if(++CurrentIndex>arrayitem.Length())
      ExitApp
   GuiControl,1:Font,% Item%CurrentIndex%
   return