How to update a DateTime gui control ??

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

How to update a DateTime gui control ??

06 Jun 2017, 07:00

Hi it's me again :(

If looked through the documentation, but cannot seem to find how to do this properly.

I have a gui that contains several DateTime control types.
I'm parsing a csv file that contains dates that are formatted like: 16-7-1976.

I would like to feed these dates to the gui using guicontrol.

Code: Select all

Gui, Add, Text, x642 y162 w100 h20 , Datum in dienst:
Gui, Add, DateTime, x642 y182 w140 h20 vDatumInDienst, d-M-yyyy

Gui, Show, Maximize
Return
Here's what I tried:

Code: Select all

MyListView:
GuiControl, Choose , DatumInDienst , %data10%, DateTime
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to update a DateTime gui control ??

06 Jun 2017, 10:36

DateTime/MonthCal: Specify for Param3 a date-time stamp in YYYYMMDDHH24MISS format. Specify %A_Now% to use the current date and time (today). For DateTime controls, Param3 may be omitted to cause the control to have no date/time selected (if it was created with that ability). For MonthCal controls, a range may be specified if the control is multi-select.

Code: Select all

GuiControl, , DatumInDienst, % data10 
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

06 Jun 2017, 10:59

Thanks, but nog sigar this time :)

The date stays at todays date.
If I put DatumInDienst in a MsgBox, there's no input.
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: How to update a DateTime gui control ??

06 Jun 2017, 11:04

What is the format of data10? It should be a YYYYMMDD format. Take a look at BoBo's answer closely.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

06 Jun 2017, 11:11

fischgeek wrote:What is the format of data10? It should be a YYYYMMDD format. Take a look at BoBo's answer closely.
It's 16-7-1976 for instance. The data is comming from a CSV file, so how could I concert it ?
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: How to update a DateTime gui control ??

06 Jun 2017, 11:15

You'll have to do some SubString manipulation and rearrange the parts.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to update a DateTime gui control ??

06 Jun 2017, 11:52

Code: Select all

; GuiControl, , DatumInDienst, % date10 := RearrangeDate(date10)

MsgBox % date10 := RearrangeDate("20-7-1962")

RearrangeDate(date){
	Loop, Parse, Date, -				; string separator the hyphen
  		array%A_Index% := A_LoopField	; save content in pseudo array 
  	SetFormat, float, 02.0				; set month zero-padding
  	array2 += 0.0						; assign that format
  	Return % array3 array2 array1		; rearrange to return the string in YYYYMMDD-format
  	}
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: How to update a DateTime gui control ??

06 Jun 2017, 14:29

While BoBo's answer may work, I encourage you to try and tackle the problem yourself by referencing the docs and your own approach.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to update a DateTime gui control ??

06 Jun 2017, 14:49

fischgeek wrote:While BoBo's answer may work, I encourage you to try and tackle the problem yourself by referencing the docs and your own approach.
:thumbup:
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

07 Jun 2017, 08:39

Thanks fischgeek and Bobo,

I decided to take your advice, and came up with my own version to tackle the problem:

Code: Select all

StringSplit, SplitDate, data10, -
If (StrLen(SplitDate1) = 1)
{
	SplitDate1 = 0%SplitDate1%
}
If (StrLen(SplitDate2) = 1)
{
	SplitDate2 = 0%SplitDate2%
}
data10 = %Splitdate3%%SplitDate2%%SplitDate1% ; change the date fields to resemble YYYYMMDD-format
However, for some strange reason I cannot get it working if I put this code inside of a function,
like so:

Code: Select all

MyFunction(MyVar, StringToSplit, LeadingNumber, Delimiter)
{
	global ; not sure if needed, doesn't work without either though.
	MsGBox, Why Doesn't this fire ????
	StringSplit, %MyVar%, %StringToSplit%, %Delimiter%
	If (StrLen(MyVar1) = 1)
	{
		MyVar1 = 0%MyVar1%
	}
	If (StrLen(MyVar2) = 1)
	{
		MyVar1 = 0%MyVar2%
	}
}
When I call it like so:

Code: Select all

MyFunction(Splitdate, data10, 0, -)
And then look at the lines most recently exectuted, on line 190 I see the function being called,
however the MsGBox inside the function doesn't fire. I cannot figure out why.
It looks like everything inside of the function gets skipped.

Code: Select all

190: MyFunction(Splitdate, data10, 0, -)  
191: data10 = %Splitdate3%%SplitDate2%%SplitDate1%
193: GuiControl,,DatumInDienst,data10
194: GuiControl,Choose,DatumUitDienst,%data11%
195: GuiControl,,Huisarts,%data12%
196: GuiControl,,Huisartsnummer,%data13%
197: GuiControl,,Contact,%data14%
198: GuiControl,,Contactnummer,%data15%
199: GuiControl,,BGG,%data16%
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to update a DateTime gui control ??

07 Jun 2017, 09:12

I decided to take your advice, and came up with my own version to tackle the problem:
:clap: :thumbup: :clap:

1) Have you read the functions/expressions page already, and how both of it are dealing with "strings" & variables?

Code: Select all

MyFunction(Splitdate,data10,0,"-")
2) as you prefer your static zero-padding aproach over using SetFormat ... :silent:

Code: Select all


F3::MsgBox % newDate := MyFunction(data10,"-")			; press F3 to call the function

MyFunction(date,delimiter) {							; convert dd-M-yyyy to yyyyMMdd
	MyVar := StrSplit(date,delimiter)
	MyVar[2] := (StrLen(MyVar[2]) = 1) ? "0" . MyVar[2] : MyVar[2]		; if myVar contains only one digit put a "0" in front of it 
	Return % myVar[3] . MyVar[2] . MyVar[1]								; rearrange the data to a compatible "Gui, DateTime"-format
	}
Tested.

3)

Code: Select all

190: 
191:
193: GuiControl,,DatumInDienst, % newDate := MyFunction(data10,"-")
Not tested :!:


PS. @fishgeek - Thx for that additional description(s)/support. Much appreciated :thumbup: 8-)
FYI. I had to correct the ternary operator (the one you've quoted) bc the outcome of StrSplit() needs to be handled differently. I wasn't aware of that till now. :oops:
Last edited by BoBo on 07 Jun 2017, 10:57, edited 11 times in total.
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: How to update a DateTime gui control ??

07 Jun 2017, 09:19

Also, notice in BoBo's answer, he removed the global from the function and is returning the result; assigning newDate as that result.
AutoHotkey Help wrote:Local variables are specific to a single function and are visible only inside that function. Consequently, a local variable may have the same name as a global variable and both will have separate contents. Separate functions may also safely use the same variable names.
Basically, what this means is that you can declare variables inside your function without effecting ones outside of it provided you remove the global from it. The second you put global inside your function like that, everything in there has the potential to overwrite variables elsewhere if you're not careful.

EDIT: lol. He also dropped what is probably a new concept to you in there. A ternary operator. This is basically just a fancy if/else statement. :)
BoBo wrote:

Code: Select all

MyVar2 := StrLen(MyVar2) = 1 ? "0" . MyVar2 : MyVar2
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

09 Jun 2017, 09:15

Thanks for the examples and advices both.
It's all a little bit overwhelming, but I managed to get a working function based on BoBo's recommendations.

I'm finding the 'shorthand' code a littlebit hard to understand, so I adapted the ternary stuff to something
that I will be able to understand when I read back the code after a view months :)

Still have a lot to learn, and I think baby steps are better suited for me.

I changed the function like so:

Code: Select all

MyFunction(date,delimiter, whichcontrol) 				; convert d-M-yyyy to yyyyMMdd
{					
	MyVar := StrSplit(date,delimiter)
	If (StrLen(MyVar[1]) = 1)			; if the DAY only contains one number put a "0" in front of it.
		MyVar[1] := "0" . MyVar[1]
	If (StrLen(MyVar[2]) = 1)			; if the MONTH only contains one number put a "0" in front of it.
		MyVar[2] := "0" . MyVar[2]
		
	newDate := % myVar[3] . MyVar[2] . MyVar[1] 
	GuiControl, , % whichcontrol, %newDate%          ; I put this inside the function, because otherwise it doesn't get accepted. (Because not being global ??)
}
@bobo, this didn't work for me:
GuiControl,,DatumInDienst, % newDate := MyFunction(data10,"-")

So now I'm calling it like this:

Code: Select all

MyFunction(data10, "-", "DatumInDienst")
MyFunction(data11, "-", "DatumUitDienst"
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: How to update a DateTime gui control ??

09 Jun 2017, 14:45

You should really take that GuiControl out of the function and have your function return the value for the control. You almost have BoBo's answer correct. just not quite right. Try this...

Code: Select all

GuiControl,, DatumInDienst, % MyFunction(data10) ; the return value of the function will be here
GuiControl,, DatumUitDienst, % MyFunction(data11) ; the return value of the function will be here

MyFunction(date,delimiter="-") ; default the delimiter to a hyphen but overridable		
{					
	MyVar := StrSplit(date,delimiter)
	If (StrLen(MyVar[1]) = 1)			; if the DAY only contains one number put a "0" in front of it.
		MyVar[1] := "0" . MyVar[1]
	If (StrLen(MyVar[2]) = 1)			; if the MONTH only contains one number put a "0" in front of it.
		MyVar[2] := "0" . MyVar[2]
		
	newDate := % myVar[3] . MyVar[2] . MyVar[1] 
	return newDate 
}
What BoBo was doing with his MsgBox, % newDate := MyFunction(data10, "-")[/code] was doing two things at once. 1: assigning the result of MyFunction into the newDate variable and then displaying the result in a MsgBox. It's a good tip for inspecting a variable's contents.

You're doing great! Keep it up and don't get discouraged. We're here to help!
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

09 Jun 2017, 16:59

Ah, sooo close :lol:

Now I finally understand what return actually does :bravo:

Great stuff. I really love this community.
I have had very bad experiences on other technical forums where everbody feels god-like and only tell you to RTFM.

I have already learned a lot from this forum and everyone is extremely helpful and patient.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

20 Jun 2017, 02:30

Hmm. Still a question.

This runs fine:

Code: Select all

SwitchAllControls(enable)
{
	GuiControl, %enable%, Voornaam
	GuiControl, %enable%, Achternaam
	GuiControl, %enable%, Status
	GuiControl, %enable%, Geboortedatum
	GuiControl, %enable%, Geslacht
	GuiControl, %enable%, Adres
	GuiControl, %enable%, Postcode
	GuiControl, %enable%, Woonplaats
	GuiControl, %enable%, Toevoeging
	GuiControl, %enable%, Telefoonnummer
	GuiControl, %enable%, Mobielnummer
	GuiControl, %enable%, Emailadres
	GuiControl, %enable%, DatumInDienst
	GuiControl, %enable%, DatumUitDienst
	GuiControl, %enable%, Dienstverband
	GuiControl, %enable%, Uitzendnummer
	GuiControl, %enable%, Uren
	GuiControl, %enable%, Maandag
	GuiControl, %enable%, Dinsdag
	GuiControl, %enable%, Woensdag
	GuiControl, %enable%, Donderdag
	GuiControl, %enable%, Vrijdag
	GuiControl, %enable%, Huisarts
	GuiControl, %enable%, Huisartsnummer
	GuiControl, %enable%, Contact
	GuiControl, %enable%, Contactnummer
	GuiControl, %enable%, BGG
	GuiControl, %enable%, BGGnummer
	return enable
}

Code: Select all

SwitchAllControls(disable)
But when I turn on #WARN
It is saying:

Code: Select all

Warning:  This variable has not been assigned a value.

Specifically: disable  (a global variable)


--->	328: SwitchAllControls(disable)  


For more details, read the documentation for #Warn.
---------------------------
OK   
---------------------------
Should I be worried about this, or can I just remove #warn, since the script does what it's supposed to do ?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to update a DateTime gui control ??

20 Jun 2017, 09:14

OK, let's guess you haven't fully absorbed that a function has to be set like an :arrow: expression

Calling a function:
SwitchAllControls() ; function call without handing over a specific content
SwitchAllControls(enable) ; handing over a variable (therefore its content has to be set somewhere else)
SwitchAllControls("enable") ; handing over a "string"
SwitchAllControls(2) ; handing over a number
var := "unbiased"
SwitchAllControls(SubStr(var,3)) ; calling a function handing over the output of an AHK function (converting "unbiased" to "biased")

Receiving function:
SwitchAllControls(enable) ; setting a variable named > enable
SwitchAllControls("enable") ; "quotation marks" arent allowed as part of a variable.
SwitchAllControls() ; function has been called without receiving a specific content
Spoiler
Last edited by BoBo on 20 Jun 2017, 11:23, edited 3 times in total.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to update a DateTime gui control ??

20 Jun 2017, 09:41

Hi Bobo,

Is just went over the code and realized myself that I had to use put disable in quotes :)
I used it to turn the guicontrol on near the top of the code, and here I did use them.

My statement that the function was working properly with #warn of was completly false :)
It didn't turn of the controls at all.

Thanks again for putting up with my mistakes :shock:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: prototype_zero and 228 guests