AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Variable Syntax Help

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
evandevon



Joined: 22 Apr 2008
Posts: 18

PostPosted: Tue May 20, 2008 5:20 pm    Post subject: Variable Syntax Help Reply with quote

Hi Forum Dwellers!

I have a gui that has 3 edit boxes that send three variables:
I want the text in each variable to be msgbox-ed out
I want to have them in a loop, 3 and use the inbuilt %A_Index% variable so that I don't have to write the same code for each msgbox. It will be useful to me later when I use many more edit boxes.

Although I know it doesn't work all I can think of is using... (problem is on the msgbox line)

Code:

Gui, Add, Edit, x36 y46 w110 h20 v1text, Enter some text
Gui, Add, Edit, x36 y76 w110 h20 v2text, Enter some text
Gui, Add, Edit, x36 y106 w110 h20 v3text, Enter some text
Gui, Add, Button, x26 y130 w100 h30 , Submit_test

Gui, Show, x131 y91 h160 w276, Variable Syntax test
Return

ButtonSubmit_test:
{
Gui, submit, NoHide
loop, 3
{
msgbox, %%A_Index%text%
}
}


GuiClose:
ExitApp


I need to somehow use a variable within a variable?
Is there some simple yet elegant way of doing this?
Thanks
_________________
Inventing problems that need solutions...
Back to top
View user's profile Send private message
chadross



Joined: 30 Apr 2008
Posts: 8
Location: Canada

PostPosted: Tue May 20, 2008 5:47 pm    Post subject: Try this Reply with quote

Is this what you're trying to accomplish?

Code:


Gui, Add, Edit, x36 y46 w110 h20 v1text, Text from 1text
Gui, Add, Edit, x36 y76 w110 h20 v2text, Text from 2text
Gui, Add, Edit, x36 y106 w110 h20 v3text, Text from 3text
Gui, Add, Button, x26 y130 w100 h30 , Submit_test

Gui, Show, x131 y91 h160 w276, Variable Syntax test
Return

ButtonSubmit_test:
{
Gui, submit, NoHide
loop, 3
{
GuiControlGet, val, ,%A_Index%text
msgbox, %val%
}
}


GuiClose:
ExitApp

_________________
Chad.
Back to top
View user's profile Send private message
Trikster



Joined: 15 Jul 2007
Posts: 1194
Location: Enterprise, Alabama

PostPosted: Tue May 20, 2008 7:08 pm    Post subject: Reply with quote

Code:

Gui, Add, Edit, x36 y46 w110 h20 v1Text, Enter some text
Gui, Add, Edit, x36 y76 w110 h20 v2Text, Enter some text
Gui, Add, Edit, x36 y106 w110 h20 v3Text, Enter some text
Gui, Add, Button, x26 y130 w100 h30 gSubmit, Submit Test
Gui, Show, x131 y91 h160 w276, Variable Syntax test
Return

Submit:
Gui, Submit, NoHide
Loop, 3 {
   MsgBox, %A_Index%%Text%
}
Return


GuiClose:
ExitApp

_________________
ScriptPad/~dieom/dieom/izwian2k7/Ian/God

Back to top
View user's profile Send private message
evandevon



Joined: 22 Apr 2008
Posts: 18

PostPosted: Wed May 21, 2008 9:18 am    Post subject: EXACTLY! Reply with quote

Thanks chadross! That's exactly what I needed! I've just looked it up in help and read that guicontrolget can grab any variable from a gui and place it in a new variable. Is that right? That's what seems to be happening.

I originally tried something like it without guicontrolget but I could never pass a variable that would increment part of its name on each loop. What would happen is that the strings "1%variable%", "2%variable%", "3%variable%" would be sent instead of text, text, text.

Thanks for your reply too Ian, I tried it but it would only send 1,2,3 too the message box rather than text1, text2, text3. I think the reason is that AHK doesn't join the two variables together to make a new variable name and get the value of that variable. So %A_Index% would be sent ok as e.g. 1 but the next variable is left as %text% instead of %1text% and since %text% has no values in it, it is left blank and so only 1,2,3 are shown.

Thanks for both your input! Evs
_________________
Inventing problems that need solutions...
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 532

PostPosted: Wed May 21, 2008 9:32 am    Post subject: Reply with quote

Change the MsgBox line in Ian's script to this:
Code:
MsgBox, % %A_Index%Text

Note the difference with regards to %
Back to top
View user's profile Send private message
evandevon



Joined: 22 Apr 2008
Posts: 18

PostPosted: Wed May 21, 2008 3:38 pm    Post subject: Reply with quote

Thanks guys! Hugo (and anyone else) could you tell me why this code doesn't work! I've tried your way and it doesn't change the output for me. The top variable %A_Index%_text is from an edit box (i.e. v1_text) and the bottom %A_Index%_program is from a Fileselectfile (i.e. 1_program). Is there some difference when working with those two variables?

The only problem with the script is that the code (simplified here) works for the top but not for the bottom one (problem code + associated code is red).

GuiControlGet, hotkey, ,%A_Index%_text
Fileappend,
%A_Index%::send %hotkey%

GuiControlGet, file, ,%A_Index%_program
Fileappend,
%A_Index%::run %file%


The Gui basically remaps 5 (2 for now) buttons (1-5) to whatever you want them to be. It appends an .ahk file, soon to be standalone .exe so that it can be more portable. I suspect there's some logic/spelling error I'm too tired to see.




Code:

;################################################
;This program is unfinished but working
;
;
;################################################
Gui, Font, Bold
Gui, Add, Tab2, x0 y0 w726 h381 , Main|Help
Gui, Tab, Main
;###############
;This is the contents of Tab Main - Button 1
;###############
Gui, Font,
Gui, Add, CheckBox, x126 y40 w17 h20 v1hotkey, 1_Button_hotkey
Gui, Add, CheckBox, x126 y76 w17 h20 v1program, 1_Button_program

Gui, Font, underline
Gui, Add, CheckBox, x126 y106 w100 h20 v1mouse, Move Mouse
Gui, Font,
Gui, Add, CheckBox, x143 y148 w86 h20 v1leftclick, Left Click?
Gui, Add, CheckBox, x143 y169 w86 h19 v1doubleclick, Double Click?

Gui, Font, underline
Gui, Add, CheckBox, x126 y195 w100 h18 v1Direction, Arrow Buttons
Gui, Font,
Gui, Add, CheckBox, x160 y210 w40 h20 v1up, Up
Gui, Add, CheckBox, x160 y230 w45 h20 v1down, Down
Gui, Add, CheckBox, x135 y220 w25 h20 v1left, L
Gui, Add, CheckBox, x205 y220 w25 h20 v1right, R

Gui, Font, underline
Gui, Add, Text, x10 y20 w110 h40 , Simple Instructions
Gui, Font,
Gui, Add, Text, x10 y40 w110 h300 , Tick ONE Main box and any ONE sub box `n`n Use 'text' to type any

text you want e.g. 'hello' or 'P'`n`n Use 'AnyFile_' to open anything e.g. a music file or Paint

program `n`n Use 'Move Mouse' to move the mouse anywhere and click or double click when it gets there

(tick a box)`n`n Use 'Arrow Buttons' to simulate the arrow keys

Gui, Add, Edit, x146 y40 w70 h20 v1_text, any text
Gui, Add, Edit, x146 y126 w30 h20 v1xmouse, X
Gui, Add, Edit, x186 y126 w30 h20 v1ymouse, Y

Gui, Font, underline
Gui, Add, Button, x146 y76 w70 h20 , AnyFile_1
Gui, Font,

Gui, Add, GroupBox, x120 y27 w115 h235 +, Button 1

;###############
;This is the contents of Tab Main - Button 2
;###############
Gui, Font,
Gui, Add, CheckBox, x241 y40 w17 h20 v2hotkey, 2_Button_hotkey
Gui, Add, CheckBox, x241 y76 w17 h20 v2program, 2_Button_program

Gui, Font, underline
Gui, Add, CheckBox, x241 y106 w100 h20 v2mouse, Move Mouse
Gui, Font,
Gui, Add, CheckBox, x258 y148 w86 h20 v2leftclick, Left Click?
Gui, Add, CheckBox, x258 y169 w86 h19 v2doubleclick, Double Click?

Gui, Font, underline
Gui, Add, CheckBox, x241 y195 w100 h18 v2Direction, Arrow Buttons
Gui, Font,
Gui, Add, CheckBox, x275 y210 w40 h20 v2up, Up
Gui, Add, CheckBox, x275 y230 w45 h20 v2down, Down
Gui, Add, CheckBox, x250 y220 w25 h20 v2left, L
Gui, Add, CheckBox, x320 y220 w25 h20 v2right, R

Gui, Add, Edit, x261 y40 w70 h20 v2_text, any text
Gui, Add, Edit, x261 y126 w30 h20 v2xmouse, X
Gui, Add, Edit, x301 y126 w30 h20 v2ymouse, Y

Gui, Font, underline
Gui, Add, Button, x261 y76 w70 h20 , AnyFile_2
Gui, Font,

Gui, Add, GroupBox, x237 y27 w115 h235 +, Button 2

;################
;This is going to be the contents of button 3
;################


Gui, Add, Button, x26 y306 w100 h30 , Create_Program
Gui, Add, Button, x226 y306 w100 h30 , Exit

Gui, Tab, Help
Gui, Add, Text, x20 y20 w110 h40 , More Instructions

Gui, Show, x131 y91 h381 w726, Project Creator v1.07
Return


ButtonAnyFile_1:
FileSelectFile, 1_program, , , 1. Choose a file,

return
ButtonAnyFile_2:
FileSelectFile, 2_program, , , 2. Choose a file,
return
ButtonAnyFile_3:
FileSelectFile, 3_program, , , 3. Choose a file,
return
ButtonAnyFile_4:
FileSelectFile, 4_program, , , 4. Choose a file,
return
ButtonAnyFile_5:
FileSelectFile, 5_program, , , 5. Choose a file,
return


ButtonCreate_Program:
{
Gui, submit, NoHide
FileSelectFile, Location, S16, , Select a Location and Filename for Your Project
}
;##############
;"Location" means both the location and filename in a single variable
;##############

;########################
;The next part appends (or creates) a file with the input fields chosen from the gui
;########################


Fileappend,
(
CoordMode, Mouse, Screen
esc::exitapp

), %location%.ahk

Loop, 5
{
;#########
;This assigns any text or hot keys to the button
;#########
If %A_Index%hotkey = 1
{
GuiControlGet, hotkey, ,%A_Index%_text
Fileappend,
(
%A_Index%::send %hotkey%
 
), %location%.ahk
}

;#########
;This assigns ANY files to the button
;#########
If %A_Index%program = 1
{
GuiControlGet, file, ,%A_Index%_program
Fileappend,
(
%A_Index%::run %file%

), %location%.ahk 
}

;###################
;These check for the direction arrow options
;###################
If %A_Index%Direction = 1
{

If %A_Index%left = 1
{
Fileappend,
(
%A_Index%::
Send {left down} 
SetTimer, WaitFor%A_Index%, 30
return

WaitFor%A_Index%:
if not GetKeyState("%A_Index%")
{
    Send {left up}
    SetTimer, WaitFor%A_Index%, off
    return
}
Send {left down} 
return

), %location%.ahk
}

If %A_Index%right = 1
{
Fileappend,
(
%A_Index%::
Send {right down} 
SetTimer, WaitFor%A_Index%, 30
return

WaitFor%A_Index%:
if not GetKeyState("%A_Index%")
{
    Send {right up}
    SetTimer, WaitFor%A_Index%, off
    return
}
Send {right down} 
return

), %location%.ahk
}

If %A_Index%up = 1
{
Fileappend,
(
%A_Index%::
Send {up down} 
SetTimer, WaitFor%A_Index%, 30
return

WaitFor%A_Index%:
if not GetKeyState("%A_Index%")
{
    Send {up up}
    SetTimer, WaitFor%A_Index%, off
    return
}
Send {up down} 
return

), %location%.ahk
}

If %A_Index%down = 1
{
Fileappend,
(
%A_Index%::
Send {down down} 
SetTimer, WaitFor%A_Index%, 30
return

WaitFor%A_Index%:
if not GetKeyState("%A_Index%")
{
    Send {down up}
    SetTimer, WaitFor%A_Index%, off
    return
}
Send {down down} 
return

), %location%.ahk
}
}

;##########################
;These check if the mouse option was chosen
;##########################

If (%A_Index%mouse == 1 AND %A_Index%leftclick == 0 AND %A_Index%doubleclick == 0)
{
GuiControlGet, mousexval, ,%A_Index%xmouse
GuiControlGet, mouseyval, ,%A_Index%ymouse
Fileappend,
(
%A_Index%::MouseMove, %mousexval%,%mouseyval%, ,
return

), %location%.ahk
}

If (%A_Index%leftclick == 1 AND %A_Index%mouse == 1)
{
GuiControlGet, mousexval, ,%A_Index%xmouse
GuiControlGet, mouseyval, ,%A_Index%ymouse
Fileappend,
(
%A_Index%::
MouseMove, %mousexval%,%mouseyval%, ,
Click
return
 
), %location%.ahk
}

If (%A_Index%doubleclick == 1 AND %A_Index%mouse == 1)
{
GuiControlGet, mousexval, ,%A_Index%xmouse
GuiControlGet, mouseyval, ,%A_Index%ymouse
Fileappend,
(
%A_Index%::
MouseMove, %mousexval%,%mouseyval%, ,
Click 2
return

), %location%.ahk
}

}



;#######################
;This loop checks the main checkboxes of each button for more than 1 tick and sends a file error

warning
;#######################
Loop, 5
{
If (%A_Index%Direction == 1 AND %A_Index%hotkey ==1 OR %A_Index%Direction == 1 AND %A_Index%program

==1 %A_Index%Direction == 1 AND %A_Index%mouse == 1 %A_Index%hotkey == 1 AND %A_Index%program == 1 OR

%A_Index%hotkey == 1 AND %A_Index%mouse == 1 OR %A_Index%program == 1 AND %A_Index%mouse == 1 OR

%A_Index%leftclick == 1 AND %A_Index%doubleclick == 1)
{
msgbox, Error! Option Conflict! `nPlease check the tick boxes for conflicts `nProject file

"%filename%" created but with errors
}
}

return

ButtonExit:
ExitApp

GuiClose:
ExitApp

_________________
Inventing problems that need solutions...
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 532

PostPosted: Wed May 21, 2008 4:38 pm    Post subject: Reply with quote

You already use Gui, Submit so I don't think you should use GuiControlGet
but simply use the variables. So instead of
Code:
GuiControlGet, hotkey, ,%A_Index%_text
try
Code:
writethistofile:=%A_Index%_text
etc if you need it in loop or simply %1_text%

You're code is a bit hard to test as it produces errors and "conflicts".
Back to top
View user's profile Send private message
evandevon



Joined: 22 Apr 2008
Posts: 18

PostPosted: Wed May 21, 2008 5:08 pm    Post subject: THANKYOU!!! Reply with quote

Many thanks Hugo!!!
I replaced the code

Code:

If %A_Index%program = 1
{
GuiControlGet, file, ,%A_Index%_program
Fileappend,
(
%A_Index%::run %file%

), %location%.ahk
}


with

Code:

If %A_Index%program = 1
{
filevar:=%A_Index%_program
Fileappend,
(
%A_Index%::run %filevar%

), %location%.ahk 
}


and it works perfectly! I think I misunderstand variable syntax. I thought that:
var1:=var2 was the same as var1=var2 or var1==var2 or var1=%var2%. I don't really understand the percentage signs even though I have read the help file. Too many late nights in front of the computer...
Thanks again!
_________________
Inventing problems that need solutions...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group