Jump to content

Recommended Posts

Posted

I made a simple script to calculate my time card for work.

For some reason, the GUICtrlSetData at the end returns error 0 every time and the $var doesn't get updated.

$var is a "dynamically" generated variable that points to one of the $diff "Label"

Apart from being really unoptimized and redundant, what am I doing wrong to get this error?

Thanks.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#Include <Date.au3>
$Form1 = GUICreate("Time card", 251, 217, 192, 124)
$Label1 = GUICtrlCreateLabel("Time card calculator", 40, 8, 126, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
$Label2 = GUICtrlCreateLabel("Jeudi", 24, 40, 29, 17)
$Label3 = GUICtrlCreateLabel("Vendredi", 8, 64, 46, 17)
$Label4 = GUICtrlCreateLabel("Lundi", 24, 88, 30, 17)
$Label5 = GUICtrlCreateLabel("Mardi", 24, 112, 30, 17)
$Label6 = GUICtrlCreateLabel("Mercredi", 8, 136, 45, 17)
$Input1 = GUICtrlCreateInput("", 56, 40, 41, 21)
$Input2 = GUICtrlCreateInput("", 56, 64, 41, 21)
$Input3 = GUICtrlCreateInput("", 56, 88, 41, 21)
$Input4 = GUICtrlCreateInput("", 56, 112, 41, 21)
$Input5 = GUICtrlCreateInput("", 56, 136, 41, 21)
$Label7 = GUICtrlCreateLabel("à", 104, 40, 10, 17)
$Label8 = GUICtrlCreateLabel("à", 104, 64, 10, 17)
$Label9 = GUICtrlCreateLabel("à", 104, 88, 10, 17)
$Label10 = GUICtrlCreateLabel("à", 104, 112, 10, 17)
$Label11 = GUICtrlCreateLabel("à", 104, 136, 10, 17)
$Input6 = GUICtrlCreateInput("", 120, 40, 41, 21)
$Input7 = GUICtrlCreateInput("", 120, 64, 41, 21)
$Input8 = GUICtrlCreateInput("", 120, 88, 41, 21)
$Input9 = GUICtrlCreateInput("", 120, 112, 41, 21)
$Input10 = GUICtrlCreateInput("", 120, 136, 41, 21)
$Button1 = GUICtrlCreateButton("Calculate", 56, 168, 105, 33)
$diff1 = GUICtrlCreateLabel("", 176, 40, 41, 21, $SS_BLACKFRAME)
$diff2 = GUICtrlCreateLabel("", 176, 64, 41, 21, $SS_BLACKFRAME)
$diff3 = GUICtrlCreateLabel("", 176, 112, 41, 21, $SS_BLACKFRAME)
$diff4 = GUICtrlCreateLabel("", 176, 88, 41, 21, $SS_BLACKFRAME)
$diff5 = GUICtrlCreateLabel("", 176, 136, 41, 21, $SS_BLACKFRAME)
GUISetState(@SW_SHOW)


GUICtrlSetData($input1, "7h30")
GUICtrlSetData($input2, "7h30")
GUICtrlSetData($input3, "7h30")
GUICtrlSetData($input4, "7h30")
GUICtrlSetData($input5, "7h30")

GUICtrlSetData($input6, "16h00")
GUICtrlSetData($input7, "16h00")
GUICtrlSetData($input8, "16h00")
GUICtrlSetData($input9, "16h00")
GUICtrlSetData($input10, "16h00")


While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Button1
            GetTime()
;~          Exit
    EndSelect
WEnd

Func GetTime()
    Dim $times[10]
    $times[0] = GUICtrlRead($input1, 1)
    $times[1] = GUICtrlRead($input2, 1)
    $times[2] = GUICtrlRead($input3, 1)
    $times[3] = GUICtrlRead($input4, 1)
    $times[4] = GUICtrlRead($input5, 1)
    $times[5] = GUICtrlRead($input6, 1)
    $times[6] = GUICtrlRead($input7, 1)
    $times[7] = GUICtrlRead($input8, 1)
    $times[8] = GUICtrlRead($input9, 1)
    $times[9] = GUICtrlRead($input10, 1)
    
;~  _ArrayDisplay($times)
    
    #cs
    compare
    0 to 5
    1 to 6
    2 to 7
    3 to 8
    4 to 9
    #ce
    
    For $i = 0 to 9
        $times[$i] = StringReplace($times[$i], "h", ":")
        $times[$i] = $times[$i] & ":00"
        $times[$i] = _NowCalcDate() & " " & $times[$i]
    Next
    
;~  _ArrayDisplay($times)
    
    Dim $diff[5]
    
    For $i = 0 to 4
        $h = $i + 5
        $diff[$i] = _DateDiff('n', $times[$i], $times[$h])
        $diff[$i] = $diff[$i] - 60
        $diff[$i] = $diff[$i] / 60
    Next

    For $i = 0 to 4
        $j = $i + 1
        $var = "$diff" & $j
        ConsoleWrite($var & " val " & $diff[$i] & " err ")
        GUICtrlSetData($var, $diff[$i])
        ConsoleWrite(@error & @CRLF)
    Next

EndFunc
Posted

GUICtrlSetData($var, $diff[$i]) is wrong because $var is not a control!

To which controls you want to write a value? $diff1 - $diff5?

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

I can think of a few better ways to do things, but I saw two problems with your code:

1. After calculating, you did not set the data to the output blocks.

2. You were using $SS_BLACKFRAME on a label. When I tried to set data to that, the block remained empty. I changed it to a read-only input box.

Below are some modifications to your script that get it working.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#Include <Date.au3>

$Form1 = GUICreate("Time card", 251, 217, 192, 124)
$Label1 = GUICtrlCreateLabel("Time card calculator", 40, 8, 126, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
$Label2 = GUICtrlCreateLabel("Jeudi", 24, 40, 29, 17)
$Label3 = GUICtrlCreateLabel("Vendredi", 8, 64, 46, 17)
$Label4 = GUICtrlCreateLabel("Lundi", 24, 88, 30, 17)
$Label5 = GUICtrlCreateLabel("Mardi", 24, 112, 30, 17)
$Label6 = GUICtrlCreateLabel("Mercredi", 8, 136, 45, 17)
$Input1 = GUICtrlCreateInput("", 56, 40, 41, 21)
$Input2 = GUICtrlCreateInput("", 56, 64, 41, 21)
$Input3 = GUICtrlCreateInput("", 56, 88, 41, 21)
$Input4 = GUICtrlCreateInput("", 56, 112, 41, 21)
$Input5 = GUICtrlCreateInput("", 56, 136, 41, 21)
$Label7 = GUICtrlCreateLabel("à", 104, 40, 10, 17)
$Label8 = GUICtrlCreateLabel("à", 104, 64, 10, 17)
$Label9 = GUICtrlCreateLabel("à", 104, 88, 10, 17)
$Label10 = GUICtrlCreateLabel("à", 104, 112, 10, 17)
$Label11 = GUICtrlCreateLabel("à", 104, 136, 10, 17)
$Input6 = GUICtrlCreateInput("", 120, 40, 41, 21)
$Input7 = GUICtrlCreateInput("", 120, 64, 41, 21)
$Input8 = GUICtrlCreateInput("", 120, 88, 41, 21)
$Input9 = GUICtrlCreateInput("", 120, 112, 41, 21)
$Input10 = GUICtrlCreateInput("", 120, 136, 41, 21)
$Button1 = GUICtrlCreateButton("Calculate", 56, 168, 105, 33)
$diff1 = GUICtrlCreateInput("", 176, 40, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff2 = GUICtrlCreateInput("", 176, 64, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff3 = GUICtrlCreateInput("", 176, 112, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff4 = GUICtrlCreateInput("", 176, 88, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff5 = GUICtrlCreateInput("", 176, 136, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUISetState(@SW_SHOW)

GUICtrlSetData($input1, "7h30")
GUICtrlSetData($input2, "7h30")
GUICtrlSetData($input3, "7h30")
GUICtrlSetData($input4, "7h30")
GUICtrlSetData($input5, "7h30")

GUICtrlSetData($input6, "16h00")
GUICtrlSetData($input7, "16h00")
GUICtrlSetData($input8, "16h00")
GUICtrlSetData($input9, "16h00")
GUICtrlSetData($input10, "16h00")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Button1
            GetTime()
;~          Exit
    EndSelect
WEnd

Func GetTime()
    Dim $times[10]
    $times[0] = GUICtrlRead($input1, 1)
    $times[5] = GUICtrlRead($input6, 1)
        GUICtrlSetData($diff1, _CalcDiff($times[0], $times[5])) 
    $times[1] = GUICtrlRead($input2, 1) 
    $times[6] = GUICtrlRead($input7, 1) 
        GUICtrlSetData($diff2, _CalcDiff($times[1], $times[6])) 
    $times[2] = GUICtrlRead($input3, 1)
    $times[7] = GUICtrlRead($input8, 1)
        GUICtrlSetData($diff3, _CalcDiff($times[2], $times[7]))
    $times[3] = GUICtrlRead($input4, 1) 
    $times[8] = GUICtrlRead($input9, 1)
        GUICtrlSetData($diff4, _CalcDiff($times[3], $times[8])) 
    $times[4] = GUICtrlRead($input5, 1)
    $times[9] = GUICtrlRead($input10, 1)
        GUICtrlSetData($diff5, _CalcDiff($times[4], $times[9]))
EndFunc

Func _CalcDiff($time1, $time2)
        $time1 = StringReplace($time1, "h", ":")
        $time1 = $time1 & ":00"
        $time1 = _NowCalcDate() & " " & $time1
        
        $time2 = StringReplace($time2, "h", ":")
        $time2 = $time2 & ":00"
        $time2 = _NowCalcDate() & " " & $time2
        
        $diff = _DateDiff('n', $time1, $time2)
        $diff = $diff - 60
        $diff = $diff / 60
    Return ($diff)
EndFunc

#include <ByteMe.au3>

Posted

Also, you could cut out 10 lines from your code by doing this:

$Input1 = GUICtrlCreateInput("7h30", 56, 40, 41, 21) ; <--- Set the data here so you do not need the next line

GUICtrlSetData($input1, "7h30")

#include <ByteMe.au3>

Posted

GUICtrlSetData($var, $diff[$i]) is wrong because $var is not a control!

To which controls you want to write a value? $diff1 - $diff5?

Br,

UEZ

So using (in the for loop)

$j = $i + 1
$var = "$diff" & $j

Doesn't make $var into a variable of that control?

Posted

I can think of a few better ways to do things, but I saw two problems with your code:

1. After calculating, you did not set the data to the output blocks.

2. You were using $SS_BLACKFRAME on a label. When I tried to set data to that, the block remained empty. I changed it to a read-only input box.

Below are some modifications to your script that get it working.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#Include <Date.au3>

$Form1 = GUICreate("Time card", 251, 217, 192, 124)
$Label1 = GUICtrlCreateLabel("Time card calculator", 40, 8, 126, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
$Label2 = GUICtrlCreateLabel("Jeudi", 24, 40, 29, 17)
$Label3 = GUICtrlCreateLabel("Vendredi", 8, 64, 46, 17)
$Label4 = GUICtrlCreateLabel("Lundi", 24, 88, 30, 17)
$Label5 = GUICtrlCreateLabel("Mardi", 24, 112, 30, 17)
$Label6 = GUICtrlCreateLabel("Mercredi", 8, 136, 45, 17)
$Input1 = GUICtrlCreateInput("", 56, 40, 41, 21)
$Input2 = GUICtrlCreateInput("", 56, 64, 41, 21)
$Input3 = GUICtrlCreateInput("", 56, 88, 41, 21)
$Input4 = GUICtrlCreateInput("", 56, 112, 41, 21)
$Input5 = GUICtrlCreateInput("", 56, 136, 41, 21)
$Label7 = GUICtrlCreateLabel("à", 104, 40, 10, 17)
$Label8 = GUICtrlCreateLabel("à", 104, 64, 10, 17)
$Label9 = GUICtrlCreateLabel("à", 104, 88, 10, 17)
$Label10 = GUICtrlCreateLabel("à", 104, 112, 10, 17)
$Label11 = GUICtrlCreateLabel("à", 104, 136, 10, 17)
$Input6 = GUICtrlCreateInput("", 120, 40, 41, 21)
$Input7 = GUICtrlCreateInput("", 120, 64, 41, 21)
$Input8 = GUICtrlCreateInput("", 120, 88, 41, 21)
$Input9 = GUICtrlCreateInput("", 120, 112, 41, 21)
$Input10 = GUICtrlCreateInput("", 120, 136, 41, 21)
$Button1 = GUICtrlCreateButton("Calculate", 56, 168, 105, 33)
$diff1 = GUICtrlCreateInput("", 176, 40, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff2 = GUICtrlCreateInput("", 176, 64, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff3 = GUICtrlCreateInput("", 176, 112, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff4 = GUICtrlCreateInput("", 176, 88, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$diff5 = GUICtrlCreateInput("", 176, 136, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUISetState(@SW_SHOW)

GUICtrlSetData($input1, "7h30")
GUICtrlSetData($input2, "7h30")
GUICtrlSetData($input3, "7h30")
GUICtrlSetData($input4, "7h30")
GUICtrlSetData($input5, "7h30")

GUICtrlSetData($input6, "16h00")
GUICtrlSetData($input7, "16h00")
GUICtrlSetData($input8, "16h00")
GUICtrlSetData($input9, "16h00")
GUICtrlSetData($input10, "16h00")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Button1
            GetTime()
;~          Exit
    EndSelect
WEnd

Func GetTime()
    Dim $times[10]
    $times[0] = GUICtrlRead($input1, 1)
    $times[5] = GUICtrlRead($input6, 1)
        GUICtrlSetData($diff1, _CalcDiff($times[0], $times[5])) 
    $times[1] = GUICtrlRead($input2, 1) 
    $times[6] = GUICtrlRead($input7, 1) 
        GUICtrlSetData($diff2, _CalcDiff($times[1], $times[6])) 
    $times[2] = GUICtrlRead($input3, 1)
    $times[7] = GUICtrlRead($input8, 1)
        GUICtrlSetData($diff3, _CalcDiff($times[2], $times[7]))
    $times[3] = GUICtrlRead($input4, 1) 
    $times[8] = GUICtrlRead($input9, 1)
        GUICtrlSetData($diff4, _CalcDiff($times[3], $times[8])) 
    $times[4] = GUICtrlRead($input5, 1)
    $times[9] = GUICtrlRead($input10, 1)
        GUICtrlSetData($diff5, _CalcDiff($times[4], $times[9]))
EndFunc

Func _CalcDiff($time1, $time2)
        $time1 = StringReplace($time1, "h", ":")
        $time1 = $time1 & ":00"
        $time1 = _NowCalcDate() & " " & $time1
        
        $time2 = StringReplace($time2, "h", ":")
        $time2 = $time2 & ":00"
        $time2 = _NowCalcDate() & " " & $time2
        
        $diff = _DateDiff('n', $time1, $time2)
        $diff = $diff - 60
        $diff = $diff / 60
    Return ($diff)
EndFunc

Wow, much more effective to write a function that way.

Makes it much less redundant and probably uses less cpu time.

I realise now that "$SS_BLACKFRAME" made my control unupdatable, thanks for the creative use of style to make it look good after!

Also, you could cut out 10 lines from your code by doing this:

$Input1 = GUICtrlCreateInput("7h30", 56, 40, 41, 21) ; <--- Set the data here so you do not need the next line

GUICtrlSetData($input1, "7h30")

Oh good point. I had set that just for testing purposes, so I'm obviously commenting them out for the "final" version.

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
×
×
  • Create New...