GUI & calculations help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ThomasPaine
Posts: 25
Joined: 24 Jun 2017, 22:17

GUI & calculations help

17 Aug 2017, 18:02

Trying to make a simple calculator and add more advanced calculations once I get the basics down. I created a small GUI where you put the price of a property, for example 100 000$ in the first edit box and then there are 2 other edit boxes that represent the amount in down payment, first one is in $ and the second is in %.

So if I put Coût de la propriété (price in $) as 100 000 and Mise de fonds (down payment in $) as 25 000, it should update the percentage box to 25% (vMiseFondsP). If I edit the percentage to 30% then it should update the down payment box in $ to 30 000$. Basically I want the edit boxes to refresh according to the last edited box. Right now I'm using Ctrl+M to do the calculations, but I'm guessing I would need a loop or something of the sort as well. Thanks!

Code: Select all

#NoEnv
#InstallMouseHook
#InstallKeybdHook
#MaxThreadsPerHotkey 2
#WinActivateForce
#SingleInstance Force

SendMode Input
DetectHiddenWindows On
SetControlDelay 1
SetWinDelay 0
SetKeyDelay, 10, 10
SetMouseDelay 1
SetBatchLines -1
SetTitleMatchMode 3

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GUI ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; Generated by AutoGUI 1.4.8a
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Text, x46 y29 w120 h23 +0x200, Coût de la propriété:
Gui Add, Text, x47 y63 w120 h23 +0x200, Mise de fonds:
Gui Add, Text, x45 y97 w120 h23 +0x200, Montant du prêt:
Gui Add, Text, x46 y131 w120 h23 +0x200, Taux d'intérêt:
Gui Add, DropDownList, x216 y165 w120, 5 ans|10 ans|15 ans|20 ans|25 ans||
Gui Add, Text, x46 y165 w120 h23 +0x200, Période d'amortissement:
Gui Add, Edit, x216 y29 w120 h23 vCout, 0
Gui Add, Edit, x216 y63 w120 h23 vMiseFonds, 0
Gui Add, Edit, x416 y63 w120 h23 vMiseFondsP, 0
Gui Add, Edit, x216 y97 w120 h23 vPret, 0
Gui Add, Edit, x216 y131 w120 h23 vTaux, 0
Gui Add, Text, x46 y203 w124 h23 +0x200, Fréquence des versements:
Gui Add, DropDownList, x216 y203 w120 vHypCourt, 1 Semaine|2 Semaines||1 Mois|

Gui Show, x503 y298 w697 h477, Window
Return

GuiEscape:
GuiClose:
    ExitApp

; Do not edit above this line

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~^m::
{
	GuiControlGet, Cout
	Tooltip, %Cout%
	
	GuiControlGet, MiseFonds
	Tooltip, %MiseFonds%
	
	GuiControlGet, MiseFondsP
	Tooltip, %MiseFondsP%
	
	GuiControlGet, Pret
	Tooltip, %Pret%
	
	GuiControlGet, Taux
	Tooltip, %Taux%
	
	GuiControlGet, HypCourt
	Tooltip, %HypCourt%
	
	GoTo, Calculations
}
return 

Calculations:
{
	Tooltip
	MiseFonds := Cout*MiseFondsP/100
	tooltip, %MiseFonds%

}
return

~PGDN::ExitApp
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: GUI & calculations help  Topic is solved

17 Aug 2017, 19:42

Consider a gLabel, two separate ones for both the MiseFonds and the MiseFondsP where if one gets changed, then the other is changed.

However, that might cause an incessant back and forth... iirc, a gLabel changing the Edit box's value triggers that Edit box's gLabel. But I don't know if this is technically a change. I think what you could do to prevent it is see if the target box has the correct value already if (MiseFonds=MiseFondsP/100*Cout) and if it is not, then you update MiseFonds to be MiseFondsP/100*Cout.

To go backwards and calculate a percent, consider Round() function to get a decimal number with 2 decimal places, and then multiply that by 100. If you only wanted whole number %'s. And what that might do though is if you input a MiseFonds value of say 25,500, which is 0.255, it would rounded to 0.26 (26%) and that value would be put in MiseFondsP, but then MiseFondsP's gLabel would be triggered and calculate that MiseFonds should be 26,000 and then it would lead to MiseFonds being changed.

But I am realizing that there's a second issue with the gLabel approach. As soon as you type in any number - so the first digit - it'll trigger. You may need a system to wait until there is no more human input, such as with this:

Code: Select all

While (A_TimeIdlePhysical<1000) ; while the user is interacting it at least once per second
   Sleep 50 ; just keep waiting

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: inseption86, jaka1, metallizer, Rohwedder and 318 guests