Page 1 of 1

Using loops and GUI together

Posted: 28 Aug 2017, 05:53
by TASan
I am kind of lost. I don't have much experience in coding, so I can't see what is wrong here. What I want to do is the following.

1. Open a date picker
2. Save the picked date in a variable
3. Open the date picker again
4. Save the picked date in another variable

Exit the loop. The goal is to be able to pick a start and end date, and get each date in a separate variable. What I have so far does not work and nags me about having a "Return" before the "}", but doing that only breaks the loop. Also, have I named this second GUI correctly since I have the same GUI earlier in the script?

Code: Select all

^T::
loopcount = 0

Loop, 2 
{
  loopcount += 1
  Gui, 2:Add, MonthCal, xDate 
  Gui, 2:Add, Button,Default,OK 
  Gui, 2:Show, x800 y200, coolgui
  WinWaitClose, coolgui
  Gui, Destroy
2ButtonOK:
Gui,Submit 
FormatTime, dato, %date%, dd.MM.yy
dato%loopcount% = %dato%
}

MsgBox % dato1
MsgBox % dato2
Return
Any help would be greatly appreciated!

Re: Using loops and GUI together

Posted: 28 Aug 2017, 07:11
by boiler
Move your 2ButtonOK subroutine to the bottom of your script, outside of the loop and hotkey subroutine (after the Return), then you can put a Return on the end of it.

Re: Using loops and GUI together

Posted: 28 Aug 2017, 07:30
by TASan
Thank you for answering, that worked in getting rid of the error :)

But I now have the problem that the date that is shown in the message box for "dato1" and "dato2" is todays date in both, and not the dates I choose in the two GUI's. I have examined the code but can't explain why?

Maybe you have some tips here too? :)

Re: Using loops and GUI together

Posted: 28 Aug 2017, 07:37
by boiler
Doesn't your Gui, Submit need to be Gui, 2:Submit? Not sure if that fixes it, but it seems like at least one problem.

Re: Using loops and GUI together

Posted: 28 Aug 2017, 07:53
by TASan
That is probably true. I changed it, but it did not do anything. I found that the following might cause a problem?

Code: Select all

Gui, 2:Add, MonthCal, xDate
Is probably should be this?

Code: Select all

Gui, 2:Add, MonthCal, vDate
But then I get an error stating that I can't have the same variable for more than one control...

Re: Using loops and GUI together

Posted: 28 Aug 2017, 09:35
by boiler
Change it to this:
Gui, 2:Add, MonthCal, vDate%loopcount%

And also change the format line to this:
FormatTime, dato, % date%loopcount%, dd.MM.yy

Re: Using loops and GUI together

Posted: 29 Aug 2017, 00:58
by TASan
Thank you so much for helping me in this, I am so close to getting this correct. The only problem now is that the second date always is today's date no matter what I choose in the second GUI that pops up. I have changed some variable names now to make the code more clear, and I would be really glad if you could perhaps look at this one more time.

Code: Select all

^T::
loopcount = 0

Loop, 2 
{
  loopcount += 1
  Gui, 2:Add, MonthCal, vDate%loopcount% 
  Gui, 2:Add, Button,Default,OK 
  Gui, 2:Show, x800 y200, coolgui
  WinWaitClose, coolgui
  Gui, Destroy
}

MsgBox % newDate1
MsgBox % newDate2
Return

2ButtonOK:
Gui, 2:Submit 
FormatTime, dato, % date%loopcount%, dd.MM.yy
newDate%loopcount% = %dato%
Return
Thanks!

Re: Using loops and GUI together

Posted: 29 Aug 2017, 01:44
by obeeb
You forgot 2 in destroy change your code to: Gui, 2:Destroy and everything will work.

Re: Using loops and GUI together

Posted: 29 Aug 2017, 01:50
by TASan
You guys are just amazing!!

Thanks, it all works!