Simple GUI question about updating Text box

Ask gaming related questions (AHK v1.1 and older)
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Simple GUI question about updating Text box

18 Jan 2017, 21:11

I imagine this is simple, but I can't get my Text box on my GUI to update. No part of the GUI is updating. I tried separating the code out and making a simple test function, but this also doesn't work.

Code: Select all

; GUI initialization
Gui, Destroy
Gui, GoW_Helper: New
Gui, Font, s14
Gui, Add, ListView, vMovesLV r10 w750 Count10 Grid -Multi , Score|Move|Xtra Turn
LV_ModifyCol(2,60)
Gui, Add, Text, vStatusBar w750 r2, Initializing GUI
Gui, Show, X1700 y500 NoActivate

F9::
updateStatusBox("My Turn1")
updateStatusBox("My Turn2")
MsgBox Hi
return

updateStatusBox(newStatus)
{
	GuiControlGet, contents,, StatusBar
	GuiControl,, StatusBar, %  newStatus "`n" contents
}
If I take out the "F9", then the script works and displays "MY Turn1" "My turn 2" Using a hotkey, nothing happens and the text box always just displays "Initializing GUI". It does pop up the MsgBox though.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Simple GUI question about updating Text box

18 Jan 2017, 23:39

Try adding Gui, GoW_Helper: Default:

Code: Select all

updateStatusBox(newStatus)
{
    Gui, GoW_Helper: Default
	GuiControlGet, contents,, StatusBar
	GuiControl,, StatusBar, %  newStatus "`n" contents
}
I hope that helps.
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: Simple GUI question about updating Text box

19 Jan 2017, 12:20

Thank you very much. That fixed it, but I would love to understand why. My script was working, and updating my GUI just fine, then I added a few more places where it should update, and it stopped updating completely. I must have made some other small change that I can't remember, but I certainly never had that line in there before, so I'm confused as to why that is suddenly the solution. Is there some other way to set up GUIs where you don't need to include that line? I tried changing my initialization to be just "Gui, New" since I only have 1 GUI and didn't actually need the label, but that still didn't work. I also put global definitions for the control names at the top of my code, but that didn't work (I actually always had them but forgot to include them in my example).

Code: Select all

; GUI Globals
global GoW_Helper
global MovesLV
global StatusText
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: Simple GUI question about updating Text box

19 Jan 2017, 13:42

Thanks again, but I'm afraid I'm still confused after reading that thread.
You said, "GuiControl does not work on unnumbered and unnamed Gui's without the default Gui being set correctly."
But in my case, wasn't my GUI named "GoW_Helper"?
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: Simple GUI question about updating Text box

19 Jan 2017, 15:05

Ok, I think I sort of figured it out, by naming my Gui "GoW_Helper" I now needed to specify the GUI name somehow in the
GuiControlGet, contents,, StatusBar
call. I have no idea how to do this, and I tried what it said in the documentation, but it failed:
GuiControlGet, contents,, StatusBar, GoW_Helper
I'm not stuck, because I can just use your suggestion, but it seems like the best way to go is to just never name your GUI.

Similarly, I added:
GuiClose:
MsgBox Hi
ExitApp
And the GuiClose function never gets called. But if I use
GoW_HelperGuiClose:
ExitApp
it works.

This is one of the things I hate about AHK. By letting people make a GUI with no name and having a bunch of defaults, it makes it way more confusing to code when you actually want to keep track of things. If you always had to make a Gui with Gui, Name: New, the documentation would be much more straightforward.
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Simple GUI question about updating Text box

19 Jan 2017, 15:18

@wolf_II , script works fine
another example , modified guicontrol

Code: Select all

Gui, GoW_Helper: New
Gui, Font, s14
Gui, Add, ListView, vMovesLV r10 w750 Count10 Grid -Multi , Score|Move|Xtra Turn
LV_ModifyCol(2,60)
Gui, Add, Text, vStatusBar w750 r2, Initializing GUI
Gui, Show, X700 y400 NoActivate
return

GoW_HelperGuiclose:
exitapp

F9::
updateStatusBox("My Turn-1`nMy Turn-2")
msgbox, 262208,OK ,Text_Updated,1
return

updateStatusBox(newStatus)
  {
  GuiControlGet, contents,, StatusBar
  GuiControl,GoW_Helper:, StatusBar, %  newStatus "`n" contents
  }
;=======================================================
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Simple GUI question about updating Text box

19 Jan 2017, 19:45

@Entropy: Maybe you could use Gui, 1: New if you wanted to set the Gui's name in such a way as to avoid the need for taking care of where non-Gui threads will need the default Gui to be set correctly.

I agree with you, If there is just a single Gui in your script, it's easier to not use Gui, New at all.
If there is a single Gui in your script, and you want to use Gui, New, then its easiest to name your Gui 1: (well, that's technically numbering, I guess)
I often use lines similar to this: Gui, 1: New, +AlwaysOnTop +Owner, %AppName% for the sake of setting the title together with setting the Gui options.
Entropy wrote:You said, "GuiControl does not work on unnumbered and unnamed Gui's without the default Gui being set correctly."
But in my case, wasn't my GUI named "GoW_Helper"?
Right, but ....
GuiControl does not work on any Gui without the default Gui being set correctly, other than ...
GuiControl only works on the Gui that is named/numbered 1:, without the default Gui being set correctly.
Or, with the default Gui being set correctly GuiControl will work correctly.
Or, still one more freedom: You don't have to set the default Gui if you don't want to, (maybe your next 5 lines of code will concern a different Gui anyway)
you can choose to tell each GuiControl which Gui to target, see garry's example. (@garry: don't you have to also tell GuiControlGet which Gui to target? I'm sure it is needed)

Try this:

Code: Select all

n=1 ; this helps to visualize later

Gui, GoW_Helper: New,, GoW Helper
Gui, Font, s14
Gui, Add, ListView, vMovesLV r10 w750 Count10 Grid -Multi , Score|Move|Xtra Turn
LV_ModifyCol(2,60)
Gui, Add, Text, vStatusBox w750 r4, Initializing GUI ; <<<<<< 4 rows for sake of visualizing
Gui, Show, X700 y400 NoActivate
return

GoW_HelperGuiclose:
exitapp

F9:: ; visualizer
    updateStatusBox(n++)
return

updateStatusBox(newText)
{
    GuiControlGet, oldText, GoW_Helper:, StatusBox
    GuiControl, GoW_Helper:, StatusBox, % newText "`n" oldText
}
I hope that helps.

Note: I believe that GuiControlGet needs to be told the "GuiNameOrNumber" inside the second parameter, since it is a command with first parameter = OutputVar.
Other GuiXXX commands expect the "GuiNameOrNumber" inside the first parameter.
I don't actually know for sure, I always play around with this and use examples that can show me what's going on.

PS: I have changed the variable name for the "StatusBox" to StatusBox, to match with the function's name.

PS2: If you're still uncertain about this, don't hesitate to ask again, I'm not sure if I used the right words or if I constructed my sentences correctly, but I tried.
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Simple GUI question about updating Text box

21 Jan 2017, 15:34

@wolf_II wrote
... you can choose to tell each GuiControl which Gui to target, see garry's example. (@garry: don't you have to also tell GuiControlGet which Gui to target? I'm sure it is needed)
yes sorry , shoul'd be needed , like in your example

Code: Select all

;....
updateStatusBox(newText)
{
    GuiControlGet, oldText, GoW_Helper:, StatusBox
    GuiControl, GoW_Helper:, StatusBox, % newText "`n" oldText
}

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 111 guests