Jump to content

Simple GUI Program


Recommended Posts

Hey, I've written this simple program to calculate the hypotenuse of a triangle but it does not display the value in the input box. There's probably something simple wrong with my syntax as i'm trying to pick up the autoit language again after not using it in about a year :) Also for the input boxes i've set them to only allow numbers however i'd like decimals to be able to be used aswell.

Could anyone point me in the right direction.

Thanks in advance,

CodeFox ;)

; Checks to see if more than one WinManager is running
$g_szVersion = "WinManager v0.1"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)

#include <GUIConstants.au3>

HotKeySet("^!x", "Quit")

$ES_NUMBER = 0x2000
Dim $WinWidth, $WinHeight, $WinHyp
$WinWidth = 0
$WinHeight = 0
$WinHyp = 0

GUICreate("WinManager - The Onsite Measurement Checker", 450 , 300 ); Creates a GUI in the center of the screen

Opt("GUICoordMode",1)
$WinWidth = GUICtrlCreateEdit ( "", 175, 15 , 100 , 25, $ES_NUMBER )
$WinHeight = GUICtrlCreateEdit ( "", 175, 50 , 100 , 25, $ES_NUMBER )
GUICtrlCreateEdit ( ""& $WinHyp , 175, 125 , 100 , 25, $ES_NUMBER )
$Button_1 = GUICtrlCreateButton ("Calculate",  175, 85, 100, 25)
$Button_2 = GUICtrlCreateButton ( "Help",  120, 175, 100, 25)
$Button_3 = GUICtrlCreateButton ( "Exit",  225, 175, 100 , 25)

GUISetState ()  ; Displays GUI

; Runs the GUI until manually closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            WinHypotenuse($WinWidth, $WinHeight); Calcualte button pressed and the hypo is calculated
        Case $msg = $Button_2
            MsgBox(0, 'Help', 'Using WinManager is simple and easy!')  ; Help button is pressed and message box appears with details
        Case $msg = $Button_3
            $ans = MsgBox(32+4, 'Exit WinManager', 'Are You Sure You Want To Exit WinManager?')  ; Exit button is pressed. The user is asked if they are sure they want to exit
            If $ans = 6 Then
            $msg = $GUI_EVENT_CLOSE
            ExitLoop
            ElseIf $ans = 7 Then
            ContinueLoop
            EndIf
    EndSelect
Wend

Func WinHypotenuse($WinWidth, $WinHeight)
     $WinHyp = $WinWidth*$WinWidth+$WinHeight*$WinHeight;
     return sqrt($WinHyp);
Endfunc

Func Quit()
    Exit 
EndFunc
Edited by CodeFox
Link to comment
Share on other sites

; Checks to see if more than one WinManager is running
$g_szVersion = "WinManager v0.1"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)

#include <GUIConstants.au3>

HotKeySet("^!x", "Quit")


Dim $WinWidth, $WinHeight, $WinHyp
$WinWidth = 0
$WinHeight = 0
$WinHyp = 0

GUICreate("WinManager - The Onsite Measurement Checker", 450 , 300 ); Creates a GUI in the center of the screen

Opt("GUICoordMode",1)
$WinWidth = GUICtrlCreateEdit ( "", 175, 15 , 100 , 25, $ES_NUMBER )
$WinHeight = GUICtrlCreateEdit ( "", 175, 50 , 100 , 25, $ES_NUMBER )
$results=GUICtrlCreatelabel ( "Results here", 175, 125 , 100 , 25 )
$Button_1 = GUICtrlCreateButton ("Calculate",  175, 85, 100, 25)
$Button_2 = GUICtrlCreateButton ( "Help",  120, 175, 100, 25)
$Button_3 = GUICtrlCreateButton ( "Exit",  225, 175, 100 , 25)

GUISetState ()  ; Displays GUI

; Runs the GUI until manually closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            WinHypotenuse($WinWidth, $WinHeight); Calcualte button pressed and the hypo is calculated
        Case $msg = $Button_2
            MsgBox(0, 'Help', 'Using WinManager is simple and easy!')  ; Help button is pressed and message box appears with details
        Case $msg = $Button_3
            $ans = MsgBox(32+4, 'Exit WinManager', 'Are You Sure You Want To Exit WinManager?')  ; Exit button is pressed. The user is asked if they are sure they want to exit
            If $ans = 6 Then
            $msg = $GUI_EVENT_CLOSE
            ExitLoop
            ElseIf $ans = 7 Then
            ContinueLoop
            EndIf
    EndSelect
Wend

Func WinHypotenuse($WinWidth, $WinHeight)
     $WinHyp = $WinWidth*$WinWidth+$WinHeight*$WinHeight;
     
     GUICtrlSetData($results,$WinHyp)
Endfunc

Func Quit()
    Exit 
EndFunc

NOT TESTED

Link to comment
Share on other sites

$WinHyp = $WinWidth*$WinWidth+$WinHeight*$WinHeight;

GUICtrlSetData($results,$WinHyp)

You forgot the square root, plus it's good practice to have it return value rather than perform that operation in the function. I would do it like this:

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            GuiCtrlSetData($results,WinHypotenuse($WinWidth, $WinHeight)); Calcualte button pressed and the hypo is calculated

[...]

Func WinHypotenuse($WinWidth, $WinHeight)
     $WinHyp = $WinWidth*$WinWidth+$WinHeight*$WinHeight;
     return sqrt($WinHyp);
Endfunc

edit: forgot code tags

Edited by pacman1176
Link to comment
Share on other sites

Thanks guys - I'll test the modifications you've posted.

Anyone know how to allow decimals to be entered or is it best to just leave the input open to anything to be able to do that?

Edited by CodeFox
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...