Jump to content
Sign in to follow this  
MrNumbers

"UpDown" control not being set properly?

Recommended Posts

MrNumbers

Hey there! I'm a little lost here... Long story short I'm trying to make a mini program to keep track of my hours at work - the code below had already been stripped down considerably, but leaves at least the main GUI intact.

The problem here is with both of the "UpDown" controls/inputbox controls. I have wrote a mini function called "IntToTime" located at the bottom of the script to change the default output of the UpDown control (0,1,2,3,4,5,etc..) into (12:00 Am, 12:30 Am, 1:00 Am .... 4:30 Pm, 5:00 Pm, etc...) The problem is though... well let me describe it:
Date is set to "27/02/2014", I set the times to "8:00 Am" and "1:00 Pm", I then change the date to "28/02/2014" which will reset both the times back to "12:00 Am". Although, now when I press Up on either of the UpDown controls, they won't go to "12:30 Am", but rather "8:30 Am" or "1:30 Pm" - same with the down buttons. Any help here on why this is happening? I've been messing around with this for hours now and it's driving me nuts :P

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <File.au3>
#include <Date.au3>
#Include <Array.au3>
Opt("GUIOnEventMode", 1)


$Form1 = GUICreate("Hour Manager v1", 243, 281, 267, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$DateSelect = GUICtrlCreateMonthCal("", 8, 8, 229, 164)
GUICtrlSetOnEvent(-1, "DateSelectChange")
$Label1 = GUICtrlCreateLabel("Time Worked:", 8, 176, 71, 17)
$StartTime = GUICtrlCreateInput("12:00 Am", 16, 200, 73, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
$Updown1 = GUICtrlCreateUpdown($StartTime)
GUICtrlSetLimit(-1, 47, 0)
GUICtrlSetOnEvent(-1, "Updown1Change")
$Label2 = GUICtrlCreateLabel("To", 112, 200, 17, 17)
$EndTime = GUICtrlCreateInput("12:00 Am", 168, 200, 73, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
$Updown2 = GUICtrlCreateUpdown($EndTime)
GUICtrlSetLimit(-1, 47, 0)
GUICtrlSetOnEvent(-1, "Updown2Change")
$Checkbox1 = GUICtrlCreateCheckbox("Worked This Day", 128, 176, 105, 17)
$Button = GUICtrlCreateButton("Open Total Time Worked", 48, 248, 145, 25)
$Label3 = GUICtrlCreateLabel("Hours Worked This Week (Sun-Sat):", 8, 224, 178, 17)
$Label4 = GUICtrlCreateLabel("0", 192, 224, 10, 17)
GUISetState(@SW_SHOW)

global $SaveDir = @ScriptDir & "\Saved Hours\",$StartVar,$EndVar,$DateArray,$Force
AdLibRegister("ForceUpdate",100)


While 1
    Sleep(100)
WEnd

Func Form1Close()
    Exit
EndFunc

Func DateSelectChange()
   UpdateGUI()
EndFunc


Func Updown1Change()
   $StartVar = GUICtrlRead($StartTime)
   GUICtrlSetData($StartTime, IntToTime(GUICtrlRead($StartTime)))
EndFunc


Func Updown2Change()
   $EndVar = GUICtrlRead($EndTime)
   GUICtrlSetData($EndTime, IntToTime(GUICtrlRead($EndTime)))
EndFunc

Func UpdateGUI()
      $DateArray = stringsplit(GUICtrlRead($DateSelect), "/")
      $StartVar = iniread($SaveDir & $DateArray[1] & ".ini", $DateArray[2], $DateArray[3] & "-S", "-1")
      $EndVar = iniread($SaveDir & $DateArray[1] & ".ini", $DateArray[2], $DateArray[3] & "-E", "-1")
   If $StartVar <> -1 Then
      GUICtrlSetData($StartTime, IntToTime($StartVar))
      GUICtrlSetData($EndTime, IntToTime($EndVar))
      GUICtrlSetState($Checkbox1, $GUI_CHECKED)
   Else
      $StartVar = 0
      $EndVar = 0
      GUICtrlSetData($StartTime, IntToTime(0))
      GUICtrlSetData($EndTime, IntToTime(0))
      GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
   EndIf
EndFunc

Func ForceUpdate()
   If $Force <> GUICtrlRead($DateSelect) Then
      $Force = GUICtrlRead($DateSelect)
      UpdateGUI()
   EndIf
EndFunc

Func IntToTime($InputHours)
   Local $AmPm, $Half
   If $InputHours < 24 Then
      $AmPm = 0
   Else
      $AmPm = 1
      $InputHours = $InputHours - 24
   EndIf
   If Mod($InputHours, 2) = 0 Then
      $Half = 0
   Else
      $Half = 1
      $InputHours = $InputHours - 1
   EndIf

   If $InputHours = 0 Then
      $InputHours = 24
   EndIf

   If $Half = 0 Then
      If $AmPm = 0 Then
         Return $InputHours / 2 & ":00 Am"
      Else
         Return $InputHours / 2 & ":00 Pm"
      EndIf
   Else
      If $AmPm = 0 Then
         Return $InputHours / 2 & ":30 Am"
      Else
         Return $InputHours / 2 & ":30 Pm"
      EndIf
   EndIf
EndFunc

Oh, I have also tried setting the values of both the inputbox and the updown controls to 0 first before changing it to "12:00 Am", yet it does not work.

Also will note, I think the problem lies @ lines 73/74:

GUICtrlSetData($StartTime, IntToTime(0))
GUICtrlSetData($EndTime, IntToTime(0))
 
I have tried setting them to 
GUICtrlSetData($StartTime, 0)
GUICtrlSetData($EndTime, 0)
Which DOES in fact, fix the problem. Although I'd really like to have them display the "12:00 Am" rather than "0"... :
Edited by MrNumbers

Share this post


Link to post
Share on other sites
Melba23

MrNumbers,

The problem seems to arise because you are setting the input content to a non-numeric value which is not recognised by the UpDown - testing the content of the input when the UpDown is used shows that it is just altering by 1 each time and not being reset when you change the date string.

My solution would be to use a separate dummy input to hold the UpDown which maintains this integer value and place the "time" string into the original - like this:

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <File.au3>
#include <Date.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)


$Form1 = GUICreate("Hour Manager v1", 243, 281, 267, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

$DateSelect = GUICtrlCreateMonthCal("", 8, 8, 229, 164)
GUICtrlSetOnEvent(-1, "DateSelectChange")

$Label1 = GUICtrlCreateLabel("Time Worked:", 8, 176, 71, 17)
$StartTime = GUICtrlCreateInput("12:00 Am", 16, 200, 60, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
$StartTime_Dummy = GUICtrlCreateInput(0, 76, 200, 16, 21) ; Dummy to hold UpDown <<<<<<<<<<<<<<<<<<<<
$Updown1 = GUICtrlCreateUpdown($StartTime_Dummy)
GUICtrlSetLimit(-1, 47, 0)
GUICtrlSetOnEvent(-1, "Updown1Change")

$Label2 = GUICtrlCreateLabel("To", 112, 200, 17, 17)
$EndTime = GUICtrlCreateInput("12:00 Am", 158, 200, 60, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
$EndTime_Dummy = GUICtrlCreateInput(0, 218, 200, 16, 21)
$Updown2 = GUICtrlCreateUpdown($EndTime_Dummy)
GUICtrlSetLimit(-1, 47, 0)
GUICtrlSetOnEvent(-1, "Updown2Change")

$Checkbox1 = GUICtrlCreateCheckbox("Worked This Day", 128, 176, 105, 17)
$Button = GUICtrlCreateButton("Open Total Time Worked", 48, 248, 145, 25)
$Label3 = GUICtrlCreateLabel("Hours Worked This Week (Sun-Sat):", 8, 224, 178, 17)
$Label4 = GUICtrlCreateLabel("0", 192, 224, 10, 17)
GUISetState(@SW_SHOW)

Global $SaveDir = @ScriptDir & "\Saved Hours\", $StartVar, $EndVar, $DateArray, $Force
AdlibRegister("ForceUpdate", 100)


While 1
    Sleep(100)
WEnd

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

Func DateSelectChange()
    UpdateGUI()
EndFunc   ;==>DateSelectChange


Func Updown1Change()
    GUICtrlSetData($StartTime, IntToTime(GUICtrlRead($StartTime_Dummy))) ; Read dummy <<<<<<<<<<<<<<<<<<<<
EndFunc   ;==>Updown1Change


Func Updown2Change()
    GUICtrlSetData($EndTime, IntToTime(GUICtrlRead($EndTime_Dummy))) ; Read dummy <<<<<<<<<<<<<<<<<<<<
EndFunc   ;==>Updown2Change

Func UpdateGUI()
    $DateArray = StringSplit(GUICtrlRead($DateSelect), "/")
    $StartVar = IniRead($SaveDir & $DateArray[1] & ".ini", $DateArray[2], $DateArray[3] & "-S", "-1")
    $EndVar = IniRead($SaveDir & $DateArray[1] & ".ini", $DateArray[2], $DateArray[3] & "-E", "-1")
    If $StartVar <> -1 Then
        GUICtrlSetData($StartTime, IntToTime($StartVar))
        GUICtrlSetData($EndTime, IntToTime($EndVar))
        GUICtrlSetState($Checkbox1, $GUI_CHECKED)
    Else
        $StartVar = 0
        $EndVar = 0
        GUICtrlSetData($StartTime, IntToTime(0))
        GUICtrlSetData($EndTime, IntToTime(0))
        GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
    EndIf
EndFunc   ;==>UpdateGUI

Func ForceUpdate()
    If $Force <> GUICtrlRead($DateSelect) Then
        $Force = GUICtrlRead($DateSelect)
        GUICtrlSetData($StartTime_Dummy, 0) ; Reset dummy <<<<<<<<<<<<<<<
        GUICtrlSetData($EndTime_Dummy, 0) ; Reset dummy <<<<<<<<<<<<<<<
        UpdateGUI()
    EndIf
EndFunc   ;==>ForceUpdate

Func IntToTime($iInput) ; New function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    Local $iHours = Mod(Int($iInput / 2), 12)
    If $iHours = 0 Then $iHours = 12
    Local $iHalf = Mod($iInput, 2)
    If $iHalf Then
        $iHalf = ":30"
    Else
        $iHalf = ":00"
    EndIf
    Switch $iInput
        Case 0 To 23
            $sSuffix = " AM"
        Case Else
            $sSuffix = " PM"
    EndSwitch

    Return $iHours & $iHalf & $sSuffix

EndFunc   ;==>IntToTime
That way when you reset the dummy on a date change everything runs as it should. :)

The IniRead section will obviously need changing, but that should not be insurmountable - let me know if I can help sort it out. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
MrNumbers

Aha! I was gonna mess around with dummy controls a bit - though considering how tired I was yesterday I just couldn't wrap my head around it all :P I also see you re-wrote my super sloppy TimeToInt command, I was gonna get around to doing that but considering it "Worked" I didn't bother, heh.

Thanks a bunch though! Works great now :)

Share this post


Link to post
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
Sign in to follow this  

×