Function help. Var in dynamic GUI control. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Function help. Var in dynamic GUI control.  Topic is solved

09 May 2021, 10:06

Sorry about the long script. This was working until I tried to make part of it into a function.

Here's a condensed version of the error message:
---------------------------
Error: A control's variable must be global or static.
Specifically: vCheckbox1
Line#
---> 227: Gui,o:Add,CheckBox,Checked%fchar% vCheckbox%a_index%,%item%
---------------------------
You'll see through the code, I predefined some of the variables so they'd be global, and thus accessible to the function. I can't figure out how to make vCheckbox...global though. I tried putting global vChecbox1 := "" near the top of the function, but that doesn't work. Even if it did work, I assume that it would still throw an error when it got to "vCheckbox2."

Ideas? Maybe I should just keep this in the code "in-line" rather than a separate function?

Spoiler
ste(phen|ve) kunkel
User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Function help. Var in dynamic GUI control.

09 May 2021, 12:21

Add Global to the top of your function
Like this

Code: Select all

myOptionsFunc(MyEntry){
    Global
    ... ; Rest of your function script
}
Check this.
User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Function help. Var in dynamic GUI control.

09 May 2021, 12:27

kunkel321 wrote: You'll see through the code, I predefined some of the variables so they'd be global, and thus accessible to the function. I can't figure out how to make vCheckbox...global though. I tried putting global vChecbox1 := "" near the top of the function, but that doesn't work. Even if it did work, I assume that it would still throw an error when it got to "vCheckbox2."
That would work if you named the variable correctly. the "v" isn't part of the variable name, that's the option indicator in the Gui command, so when you define it as global, you have to only use the variable name itself, like this:

Code: Select all

global Checkbox1, Checkbox2 ; however many you have
Smile_ wrote: Add Global to the top of your function
Like this

Code: Select all

myOptionsFunc(MyEntry){
    Global
    ... ; Rest of your function script
}
I would not make all the variables in the function global, generally. I would as an alternative just define the specific variables global to that function as need be, which would be done by moving the global line with the specific variable names I showed above to inside the function. That makes them global to that function but not other functions, so depending on your needs, choose one option or the other.
User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Function help. Var in dynamic GUI control.

09 May 2021, 14:53

Thanks for the replies guys!

Code: Select all

global Checkbox1, Checkbox2 ; however many you have
If tried this Gui, o:Add, CheckBox, Checked%fchar% global Checkbox%a_index%, %item% but it says "Label not found." Maybe it sees global and looks for lobal: ?

Code: Select all

myOptionsFunc(MyEntry){
    Global
    ... ; Rest of your function script
}
I think this might have worked. My result didn't get returned, but I think that might be a different issue...
ste(phen|ve) kunkel
User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Function help. Var in dynamic GUI control.

09 May 2021, 15:09

Hmmm.... I put a message box right after my function (see below), and it appears that my code is not waiting for the function to return anything... It launches the message box while the GUI is still waiting for input...

Code: Select all

If InStr(MyEntry, "<Options>") ; Options tag found in expansion text.
{
	myOptionsFunc(MyEntry)
	MsgBox right after function, myEntry is: %MyEntry%
}
Also, for the last line of code (other than the 'cancel' label) I put

Code: Select all

return MyEntry := StrReplace(MyEntry,"<Options>",myOptions) ; Replace Options tag with checked items.
because that seemed appropriate.

I'm going to check out some more function examples from the interwebs.
ste(phen|ve) kunkel
User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Function help. Var in dynamic GUI control.

09 May 2021, 15:31

but it says "Label not found."
Very logic, it is caused by this global, g used to define label subroutine to be executed whenever the control is clicked or selected or changed, so just remove it or add that label (lobal) to your script and this Checkbox%a_index% should be vCheckbox%a_index% (You missing v)
It launches the message box while the GUI is still waiting for input...
Add a return after calling your function

Code: Select all

If InStr(MyEntry, "<Options>")
{
    myOptionsFunc(MyEntry)
}
Return
Also move other 2 labels (MyOpts, CancelOpts) outside your function, I think it is not need to include them inside your function, although it might be needed (I don't know the goal of the script), just I see it more convenient :) .
User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Function help. Var in dynamic GUI control.

09 May 2021, 16:15

kunkel321 wrote: If tried this Gui, o:Add, CheckBox, Checked%fchar% global Checkbox%a_index%, %item% but it says "Label not found." Maybe it sees global and looks for lobal: ?
You don't put global in your Gui command. And you don't remove v from before the variable in the Gui command. The v in the Gui command identifies it as that control's associated variable. What I was pointing out was that the v itself isn't part of the variable name, so in your global declaration, you use the variable name Checkbox1 as opposed to vCheckbox1.

The global declaration can go either at the top of your script overall (the auto-execute section) or inside your function that will use them. It is a stand-alone line, not part of your Gui command, as I showed before:

Code: Select all

global Checkbox1, Checkbox2 ; however many you have
Then your Gui line would keep the v as before:

Code: Select all

Gui, o:Add, CheckBox, Checked%fchar% vCheckbox%a_index%, %item%

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: FanaticGuru, Google [Bot] and 128 guests