Using AHK to do a calculation using variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

Using AHK to do a calculation using variables

Post by alpha-centauri » 30 Jan 2023, 07:05

Hi I am a noob. Only been a week since I started using AHK. I am trying to create a calculator that allows me to add custom input values. And changes one value using an if statement and radio buttons. I can't get it to work tho. Any help is appreciated!

Code: Select all

::kgm-calc::
Sleep, 400
Gui, Add, Text, , Var1
Gui, Add, Edit, vVar1
Gui, Add, Text, , Var2
Gui, Add, Edit, vVar2
Gui, Add, Radio, vRadio1, Radio1
Gui, Add, Radio, vRadio2, Radio2
Gui, Add, Button, Default w80, &OK
Gui, Show, w180
return

ButtonOK:
Gui, Submit, Hide
if (vRadio1 =1)
{
	vVar3:=5
}
if (vRadio2 =1)
{
	vVar3:=10
}

vResult := (%Var1%-%Var2%)*(%Var2%)*%Var3%
Send %Result%
Gui, Destroy
return

GuiClose:
Gui, Destroy
return

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Using AHK to do a calculation using variables

Post by boiler » 30 Jan 2023, 07:27

The initial v is not part of the variable name when you use the v option to signify the variable name when creating a GUI control. So it’s Radio1 when you refer to it — not vRadio1 — for example.

Also, you don’t put % signs around variables in expressions (not normally, anyway, and not in this case). And I’m not seeing why you put a v before Result when you refer to it without one on the very next line. The v is specifically an option indicator when associating a variable with a GUI control when you’re adding it to the GUI. It’s not how you initialize variables in general or something like that. So:

Code: Select all

vResult := (%Var1%-%Var2%)*(%Var2%)*%Var3%
…should be:

Code: Select all

Result := (Var1-Var2)*(Var2)*Var3
…although I don’t see why you have parentheses around Var2.

Since you’re new to AHK, I recommend using and learning v2 and don’t bother learning the quirks of things like when to use expressions vs. legacy syntax that are necessary in v1.

alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

Re: Using AHK to do a calculation using variables

Post by alpha-centauri » 30 Jan 2023, 08:57

Hey I made the change you suggested but there's no output. I'll check out AHK 2 as well. Thank you.

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Using AHK to do a calculation using variables

Post by mikeyww » 30 Jan 2023, 10:06

Code: Select all

; This script provides a calculator
#Requires AutoHotkey v2.0
check := (*) => btn.Enabled := IsNumber(v1.Value) && IsNumber(v2.Value)
                                && rad1.Value rad2.Value ? True : False
calc  := (*) => MsgBox((rad1.Value ? 5 : 10) * v2.Value * (v1.Value - v2.Value), 'Result', 64)
gui1  := Gui(, 'Calc'), gui1.SetFont('s10')
gui1.AddText('w250', 'Var1'), v1 := gui1.AddEdit('wp'), v1.OnEvent('Change', check)
gui1.AddText('wp'  , 'Var2'), v2 := gui1.AddEdit('wp'), v2.OnEvent('Change', check)
rad1  := gui1.AddRadio(, 'R1'), rad1.OnEvent('Click', check)
rad2  := gui1.AddRadio(, 'R2'), rad2.OnEvent('Click', check)
btn   := gui1.AddButton('w250 Default Disabled', 'Calc'), btn.OnEvent('Click', calc)

F3::gui1.Show

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Using AHK to do a calculation using variables

Post by boiler » 30 Jan 2023, 11:34

alpha-centauri wrote:
30 Jan 2023, 08:57
Hey I made the change you suggested but there's no output. I'll check out AHK 2 as well. Thank you.
There is output if you make all the changes I mentioned. I told you about more changes that are necessary than just the one line I corrected. You have a v prefix where it doesn't belong in several places, corrected here:

Code: Select all

::kgm-calc::
Sleep, 400
Gui, Add, Text, , Var1
Gui, Add, Edit, vVar1
Gui, Add, Text, , Var2
Gui, Add, Edit, vVar2
Gui, Add, Radio, vRadio1, Radio1
Gui, Add, Radio, vRadio2, Radio2
Gui, Add, Button, Default w80, &OK
Gui, Show, w180
return

ButtonOK:
Gui, Submit, Hide
if (Radio1 =1)
{
	Var3:=5
}
if (Radio2 =1)
{
	Var3:=10
}

Result := (Var1-Var2)*(Var2)*Var3
Send %Result%
Gui, Destroy
return

GuiClose:
Gui, Destroy
return

Post Reply

Return to “Ask for Help (v1)”