Using Variables in as Label. Possible?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pukeface
Posts: 10
Joined: 02 Mar 2016, 18:17

Using Variables in as Label. Possible?

20 Mar 2016, 09:28

Is this something that would be possible to do somehow:

Code: Select all

Var = 2

If ( Var = "1" )
{
    LabelNr = One
}
Else If ( Var = "2" )
{   
    LabelNr = Two
}

Gui, Add, Button, gLabel%LabelNr%, Press for success?!
Return

Label%LabelNr%:
Do Something Awesome?!
Return
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Using Variables in as Label. Possible?

20 Mar 2016, 14:52

Try this:

Code: Select all

Var = 2

If ( Var = "1" )
{
    LabelNr = One
}
Else If ( Var = "2" )
{
    LabelNr = Two
}

Gui, Add, Button, gLabel%LabelNr%, Press for success?!
gui, show
Return


LabelOne:
    MsgBox, Something awesome?!
Return

LabelTwo:
    MsgBox, Something else?!
Return
I hope that helps.
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Using Variables in as Label. Possible?

20 Mar 2016, 16:07

Hi Pukeface,
I know it's not what your question is, but whatever reason you need to do what you asked for, maybe the use of variables could also be a viable option:

Code: Select all

Var = 2
 
If ( Var = "1" )
{
    LabelNr = One
}
Else If ( Var = "2" )
{
    LabelNr = Two
}
 
Gui, Add, Button, v%LabelNr% gLabel, Press for success?!
gui, show
Return
 
 
Label:
If A_GuiControl = One
    MsgBox, Something awesome!
If A_GuiControl = Two
	MsgBox, Something else!	
Return
lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Using Variables in as Label. Possible?

21 Mar 2016, 02:08

@Pukeface: I think you're approaching it from the wrong angle. A label points at only a single point in the code, and your example label points at code which doesn't care which label name was used. Why should the label's name vary? If you just want to know who called the label, you can (in the general case) set a variable (e.g. just refer to LabelNr within the sub), or for a GUI just use A_GuiControl as lblb showed.

Alternatively (in the general case), you can use a function which accepts a parameter. However, it gets a little trickier with GUI controls:

Code: Select all

Var = 2
 
If ( Var = "1" )
{
    LabelNr = One
}
Else If ( Var = "2" )
{   
    LabelNr = Two
}

Gui, Add, Button, gLabel, Show global var

Gui, Add, Button, , Show parameter
fn := Func("myFunction").Bind(LabelNr)
GuiControl, +g, Button2, % fn

Gui, Show
Return

Label:
MsgBox Global var: %LabelNr%
return
 
myFunction(arg) {
	MsgBox Parameter: %arg%
}

GuiClose:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest and 347 guests