Search code examples
autoit

Calculator not resetting


I created this script to calculate between mAh and mWh. When I put in the values and press calculate, the calculations seem correct.

However, I cannot figure out how to enter new values and hit calculate without having to manually restart the script. Can someone help me figure out what is missing?

; Source(s)
; http://www.autoitscript.com/forum/topic/125495-guicreate-select-radio-button/

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=D:\Downloads\koda_2008-10-03\Forms\mWh2mAh.kxf
$Form1 = GUICreate("mAh mWh Calculator", 336, 210, 682, 127)

$capacity = GUICtrlCreateInput("", 224, 48, 81, 21)
GUICtrlSetCursor (-1, 0)

$mAh = GUICtrlCreateRadio("mAh", 224, 72, 41, 17)
GUICtrlSetCursor (-1, 0)
GUICtrlSetState ($mAh,$GUI_CHECKED)

$mWh = GUICtrlCreateRadio("mWh", 272, 72, 41, 17)
GUICtrlSetCursor (-1, 0)

$Volt=GUICtrlCreateInput("", 224, 96, 81, 21)
GUICtrlSetCursor (-1, 0)

$Calculate = GUICtrlCreateButton("Calculate", 104, 136, 75, 25)
$Input2 = GUICtrlCreateInput("", 184, 136, 121, 21)
GUICtrlSetState ($Input2, $GUI_DISABLE)
GUICtrlSetCursor (-1, 0)

$Label3 = GUICtrlCreateLabel("Calculate between milliampere-hour and milliwatt-hour", 16, 16, 304, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("Select the appropriate radio button:", 48, 72, 170, 17)
$Label1 = GUICtrlCreateLabel("Enter the mAh or mWh value for the battery:", 8, 48, 211, 17)
$Label2 = GUICtrlCreateLabel("Enter the battery voltage:", 96, 96, 123, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Calculate

            if GUICtrlRead($capacity)=0 then ExitLoop
            if GUICtrlRead($Volt)=0 then ExitLoop

            Switch GUICtrlRead($mAh)
                Case $GUI_Checked   ;mAh checked

                    $mAh = Int(GUICtrlRead($capacity))
                    $Ah = ($mAh/1000)
                    $Wh = ($Ah*(GUICtrlRead($Volt)))
                    $mWh = ($Wh*1000)
                    $myval = Int($mWh)&" mWh"

                Case Else
            EndSwitch

            Switch GUICtrlRead($mWh)
                Case $GUI_Checked   ;mWh checked

                    $mWh = Int(GUICtrlRead($capacity))
                    $Wh = ($mWh/1000)
                    $Ah = ($Wh/(GUICtrlRead($Volt)))
                    $mAh = ($Ah*1000)
                    $myval = Int($mAh)&" mAh"

                Case Else
            EndSwitch

                    GUICtrlSetData(8, string($myval))           

    EndSwitch
WEnd

Solution

  • Instead of ExitLoop use ContinueLoop... You exit the while loop else which will cause your script to terminate... It will not react to any GUI messages any more but will continue to stay open until you terminate it the hard way, because the GUI is still showing, but the main loop is left and there are no further commands to execute. And instead of having the two inner switch blocks you can just use one if-then-else-endif... Your radio group can only have two states. Last but not least you shouldn't use "8" as a reference in the GUICtrlSetData function, but $Input2... I didn't test my suggestions, it's just from reading your code what mistakes popped into my eyes. So feel free to test it and write if it doesn't work after trying our my advice... Good luck.

    EDIT: Ok, I checked it out completely and run your code. Here's my working result:

    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    
    #Region ### START Koda GUI section ### Form=D:\Downloads\koda_2008-10-03\Forms\mWh2mAh.kxf
    $Form1 = GUICreate("mAh mWh Calculator", 336, 210, 682, 127)
    
    $capacity = GUICtrlCreateInput("", 224, 48, 81, 21)
    
    $mAhRadio = GUICtrlCreateRadio("mAh", 224, 72, 41, 17)
    GUICtrlSetState ($mAhRadio, $GUI_CHECKED)
    
    $mWhRadio = GUICtrlCreateRadio("mWh", 272, 72, 41, 17)
    
    $Volt=GUICtrlCreateInput("", 224, 96, 81, 21)
    
    $Calculate = GUICtrlCreateButton("Calculate", 104, 136, 75, 25)
    $Input2 = GUICtrlCreateInput("", 184, 136, 121, 21)
    GUICtrlSetState ($Input2, $GUI_DISABLE)
    
    $Label3 = GUICtrlCreateLabel("Calculate between milliampere-hour and millwatt-hour", 16, 16, 304, 17)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Label4 = GUICtrlCreateLabel("Select the appropriate radio button:", 48, 72, 170, 17)
    $Label1 = GUICtrlCreateLabel("Enter the mAh or mWh value for the battery:", 8, 48, 211, 17)
    $Label2 = GUICtrlCreateLabel("Enter the battery voltage:", 96, 96, 123, 17)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    
    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
    
            Case $Calculate
                If GUICtrlRead($capacity) == 0 Then ContinueLoop
                If GUICtrlRead($Volt) == 0 Then ContinueLoop
    
                If GUICtrlRead($mAhRadio) == $GUI_Checked Then
                    ;mAh checked
                    $mAh = Int(GUICtrlRead($capacity))
                    $Ah = ($mAh*1000)
                    $Wh = ($Ah*(GUICtrlRead($Volt)))
                    $mWh = ($Wh/1000)
                    $myval = Int($mWh)&" mWh"
                Else
                    ;mWh checked
                    $mWh = Int(GUICtrlRead($capacity))
                    $Wh = ($mWh*1000)
                    $Ah = ($Wh/(GUICtrlRead($Volt)))
                    $mAh = ($Ah/1000)
                    $myval = Int($mAh)&" mAh"
                EndIf
                GUICtrlSetData($Input2, string($myval))           
        EndSwitch
    WEnd
    

    Your problem was: You overwrote $mAh and $mWh. You had it assigned twice. To the Radio-Button and to the variable in your calculation. So I changed that and I cleaned up your code a bit like I mentioned above. It works as expected now. Good luck!

    By the way, I took the freedom to Change your algorithm a bit. From mAh to Ah it's multiplied by 1000, right? Not divided... And vice versa. The same with mWh and Wh...