Jump to content

GUI Issues when run multiple times


 Share

Recommended Posts

Can anyone figure out why on the second run through I get the errors and why all my values start to go haywire? The first time around everything is correct. The times after that everything starts to go wacky.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>

#Region ### START Koda GUI section ### Form=c:\users\ash\desktop\programmingsheeet\mccampbell helper\sampleloop.kxf
$Form1_1 = GUICreate("Form1", 407, 190, 223, 170)
$TubingID = GUICtrlCreateInput("ID of tubing", 8, 72, 153, 21)
$Label1 = GUICtrlCreateLabel("Sample Loop Calculator", 72, 24, 207, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
$Label2 = GUICtrlCreateLabel("Inside Diameter of tubing", 224, 72, 121, 17)
$Volume = GUICtrlCreateInput("Desired Volume", 8, 104, 153, 21)
$Label4 = GUICtrlCreateLabel("Sample Loop Desired Volume", 224, 104, 143, 17)
;~ $TubingIDUnits = GUICtrlCreateList("", 168, 72, 49, 19)
$TubingIDUnits = GUICtrlCreateCombo("mm", 168, 72, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$VolumeUnits = GUICtrlCreateCombo("mL", 168, 104, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "uL|Liters")
$Label3 = GUICtrlCreateLabel("Length of tubing required is:", 16, 136, 138, 17)
$FinalUnits = GUICtrlCreateCombo("mm", 168, 136, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$Answer = GUICtrlCreateLabel("", 224, 136, 148, 17)
$GO = GUICtrlCreateButton("Calculate!", 16, 160, 121, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

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

    EndSwitch
WEnd

Func Calculate()

    If GUICtrlRead($TubingID) = "ID of tubing" Then
        MsgBox(0, "Error", "Please Enter Tubing Inside Diameter")
    Else
        $TubingID = GUICtrlRead($TubingID)
    EndIf

    If GUICtrlRead($Volume) = "Desired Volume" Then
        MsgBox(0, "Error", "Please Enter Sample Loop Volume")
    Else
        $Volume = GUICtrlRead($Volume)
    EndIf

;~ Unit Conversions
    If GUICtrlRead($TubingIDUnits) = "mm" Then $TubingID = $TubingID
    If GUICtrlRead($TubingIDUnits) = "cm" Then $TubingID = $TubingID * 10
    If GUICtrlRead($TubingIDUnits) = "inches" Then $TubingID = $TubingID * 25.4
    If GUICtrlRead($VolumeUnits) = "mL" Then $Volume = $Volume
    If GUICtrlRead($VolumeUnits) = "uL" Then $Volume = $Volume / 1000
    If GUICtrlRead($VolumeUnits) = "Liters" Then $Volume = $Volume * 1000

    ; Volume = pie * R^2 * h
    ; $Volume = ((IDtubing in mm)/2)^2  * pie * length of tubing in mm
    ;~ $TubingID = .1 ; in mm

    Local $radius = $TubingID / 2 ; in mm
    ConsoleWrite("Radius of ID Tubing is: " & $radius & "mm" & @CRLF)
    ConsoleWrite("Volume Requested is: " & $Volume & "mL" & @CRLF)
    Local $radiussq = $radius * $radius
    ConsoleWrite("Radius Squared is: " & $radiussq & @CRLF)

    Local $radiussqtimespie = $radiussq * 3.14159
    ConsoleWrite("Radius Squared times pie is: " & $radiussqtimespie & "in mm^2" & @CRLF)
    Local $cubicmillimeters = $Volume * 1000 ;1000cubic millimeters = 1mL
    ConsoleWrite("Cubic millimeters is: " & $cubicmillimeters & @CRLF)
    Global $lengthinmm = $cubicmillimeters / $radiussqtimespie
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthinmm & "'s mm of tubing" & @CRLF)
    Global $lengthininches = $lengthinmm / 25.4
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthininches & "'s inches of tubing" & @CRLF)
    GUICtrlDelete($Answer)
    If GUICtrlRead($FinalUnits) = "mm" Then $lengthinmm = $lengthinmm
    If GUICtrlRead($FinalUnits) = "cm" Then $lengthinmm = $lengthinmm / 10
    If GUICtrlRead($FinalUnits) = "inches" Then $lengthinmm = $lengthinmm / 25.4
    $Answer = GUICtrlCreateLabel($lengthinmm & " is the length in " & GUICtrlRead($FinalUnits), 224, 136, 160, 30)
EndFunc   ;==>Calculate

 

Link to comment
Share on other sites

Here is fixed script:

Mistake was in rewriting input identificators by their vaules when reading values, you have to use separate variables for IDs and values

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>

#Region ### START Koda GUI section ### Form=c:\users\ash\desktop\programmingsheeet\mccampbell helper\sampleloop.kxf
$Form1_1 = GUICreate("Form1", 407, 190, 223, 170)
$TubingID = GUICtrlCreateInput("ID of tubing", 8, 72, 153, 21)
$Label1 = GUICtrlCreateLabel("Sample Loop Calculator", 72, 24, 207, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
$Label2 = GUICtrlCreateLabel("Inside Diameter of tubing", 224, 72, 121, 17)
$VolumeID = GUICtrlCreateInput("Desired Volume", 8, 104, 153, 21)
$Label4 = GUICtrlCreateLabel("Sample Loop Desired Volume", 224, 104, 143, 17)
;~ $TubingIDUnits = GUICtrlCreateList("", 168, 72, 49, 19)
$TubingIDUnits = GUICtrlCreateCombo("mm", 168, 72, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$VolumeUnits = GUICtrlCreateCombo("mL", 168, 104, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "uL|Liters")
$Label3 = GUICtrlCreateLabel("Length of tubing required is:", 16, 136, 138, 17)
$FinalUnits = GUICtrlCreateCombo("mm", 168, 136, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$Answer = GUICtrlCreateLabel("", 224, 136, 160, 30)
$GO = GUICtrlCreateButton("Calculate!", 16, 160, 121, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

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

    EndSwitch
WEnd

Func Calculate()

    $Tubing = GUICtrlRead($TubingID)
    If $Tubing = "ID of tubing" Then
        MsgBox(0, "Error", "Please Enter Tubing Inside Diameter")
    EndIf

    $Volume = GUICtrlRead($VolumeID)
    If $Volume = "Desired Volume" Then
        MsgBox(0, "Error", "Please Enter Sample Loop Volume")
    EndIf

;~ Unit Conversions
    If GUICtrlRead($TubingIDUnits) = "mm" Then $Tubing = $Tubing
    If GUICtrlRead($TubingIDUnits) = "cm" Then $Tubing = $Tubing * 10
    If GUICtrlRead($TubingIDUnits) = "inches" Then $Tubing = $Tubing * 25.4
    If GUICtrlRead($VolumeUnits) = "mL" Then $Volume = $Volume
    If GUICtrlRead($VolumeUnits) = "uL" Then $Volume = $Volume / 1000
    If GUICtrlRead($VolumeUnits) = "Liters" Then $Volume = $Volume * 1000

    ; Volume = pie * R^2 * h
    ; $Volume = ((IDtubing in mm)/2)^2  * pie * length of tubing in mm
    ;~ $TubingID = .1 ; in mm

    Local $radius = $Tubing / 2 ; in mm
    ConsoleWrite("Radius of ID Tubing is: " & $radius & "mm" & @CRLF)
    ConsoleWrite("Volume Requested is: " & $Volume & "mL" & @CRLF)
    Local $radiussq = $radius * $radius
    ConsoleWrite("Radius Squared is: " & $radiussq & @CRLF)

    Local $radiussqtimespie = $radiussq * 3.14159
    ConsoleWrite("Radius Squared times pie is: " & $radiussqtimespie & "in mm^2" & @CRLF)
    Local $cubicmillimeters = $Volume * 1000 ;1000cubic millimeters = 1mL
    ConsoleWrite("Cubic millimeters is: " & $cubicmillimeters & @CRLF)
    Global $lengthinmm = $cubicmillimeters / $radiussqtimespie
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthinmm & "'s mm of tubing" & @CRLF)
    Global $lengthininches = $lengthinmm / 25.4
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthininches & "'s inches of tubing" & @CRLF)
;~     GUICtrlDelete($Answer)
    If GUICtrlRead($FinalUnits) = "mm" Then $lengthinmm = $lengthinmm
    If GUICtrlRead($FinalUnits) = "cm" Then $lengthinmm = $lengthinmm / 10
    If GUICtrlRead($FinalUnits) = "inches" Then $lengthinmm = $lengthinmm / 25.4
;~     $Answer = GUICtrlCreateLabel($lengthinmm & " is the length in " & GUICtrlRead($FinalUnits), 224, 136, 160, 30)
    GUICtrlSetData($Answer, $lengthinmm & " is the length in " & GUICtrlRead($FinalUnits))
EndFunc   ;==>Calculate

 

Link to comment
Share on other sites

And here is optimized version avoiding multiple calls of GUICtrlRead() on the same control ID, removed unnecesary code and also added Round($lengthinmm,3)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>

#Region ### START Koda GUI section ### Form=c:\users\ash\desktop\programmingsheeet\mccampbell helper\sampleloop.kxf
$Form1_1 = GUICreate("Form1", 407, 190, 223, 170)
$TubingID = GUICtrlCreateInput("ID of tubing", 8, 72, 153, 21)
$Label1 = GUICtrlCreateLabel("Sample Loop Calculator", 72, 24, 207, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
$Label2 = GUICtrlCreateLabel("Inside Diameter of tubing", 224, 72, 121, 17)
$VolumeID = GUICtrlCreateInput("Desired Volume", 8, 104, 153, 21)
$Label4 = GUICtrlCreateLabel("Sample Loop Desired Volume", 224, 104, 143, 17)
;~ $TubingIDUnits = GUICtrlCreateList("", 168, 72, 49, 19)
$TubingUnitsID = GUICtrlCreateCombo("mm", 168, 72, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$VolumeUnitsID = GUICtrlCreateCombo("mL", 168, 104, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "uL|Liters")
$Label3 = GUICtrlCreateLabel("Length of tubing required is:", 16, 136, 138, 17)
$FinalUnitsID = GUICtrlCreateCombo("mm", 168, 136, 49, 19, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "cm|inches")
$Answer = GUICtrlCreateLabel("", 224, 136, 160, 30)
$GO = GUICtrlCreateButton("Calculate!", 16, 160, 121, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GO
            Calculate()
    EndSwitch
WEnd

Func Calculate()

    $Tubing = GUICtrlRead($TubingID)
    If $Tubing = "ID of tubing" Then
        MsgBox(0, "Error", "Please Enter Tubing Inside Diameter")
    EndIf

    $Volume = GUICtrlRead($VolumeID)
    If $Volume = "Desired Volume" Then
        MsgBox(0, "Error", "Please Enter Sample Loop Volume")
    EndIf

;~ Unit Conversions
    $TubingUnits = GUICtrlRead($TubingUnitsID)
    If $TubingUnits = "cm" Then $Tubing = $Tubing * 10
    If $TubingUnits = "inches" Then $Tubing = $Tubing * 25.4

    $VolumeUnits = GUICtrlRead($VolumeUnitsID)
    If $VolumeUnits = "uL" Then $Volume = $Volume / 1000
    If $VolumeUnits = "Liters" Then $Volume = $Volume * 1000

    ; Volume = pie * R^2 * h
    ; $Volume = ((IDtubing in mm)/2)^2  * pie * length of tubing in mm
    ;~ $TubingID = .1 ; in mm

    Local $radius = $Tubing / 2 ; in mm
    ConsoleWrite("Radius of ID Tubing is: " & $radius & "mm" & @CRLF)
    ConsoleWrite("Volume Requested is: " & $Volume & "mL" & @CRLF)
    Local $radiussq = $radius * $radius
    ConsoleWrite("Radius Squared is: " & $radiussq & @CRLF)

    Local $radiussqtimespie = $radiussq * 3.14159
    ConsoleWrite("Radius Squared times pie is: " & $radiussqtimespie & "in mm^2" & @CRLF)
    Local $cubicmillimeters = $Volume * 1000 ;1000cubic millimeters = 1mL
    ConsoleWrite("Cubic millimeters is: " & $cubicmillimeters & @CRLF)
    Local $lengthinmm = $cubicmillimeters / $radiussqtimespie
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthinmm & "'s mm of tubing" & @CRLF)
    Local $lengthininches = $lengthinmm / 25.4
    ConsoleWrite("Length required for 1000mL loop is: " & $lengthininches & "'s inches of tubing" & @CRLF)

    $FinalUnits = GUICtrlRead($FinalUnitsID)
    If $FinalUnits = "cm" Then $lengthinmm = $lengthinmm / 10
    If $FinalUnits = "inches" Then $lengthinmm = $lengthinmm / 25.4
    GUICtrlSetData($Answer, Round($lengthinmm,3) & " is the length in " & $FinalUnits)
EndFunc   ;==>Calculate

 

Edited by Zedna
Link to comment
Share on other sites

8 hours ago, Zedna said:

And here is optimized version avoiding multiple calls of GUICtrlRead() on the same control ID, removed unnecesary code and also added Round($lengthinmm,3)

 

Thanks man. I didn't think that reusing input identificators would have caused issues, but of course that makes sense. Also thank you for the optimization/cleanup. 

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...