AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help making GUI work inside a function

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
SxFtSxInch



Joined: 29 Jul 2007
Posts: 2

PostPosted: Sun Jul 29, 2007 11:25 pm    Post subject: Help making GUI work inside a function Reply with quote

I'm just trying to understand how a GUI works inside a function
& about GUI's in general. Obviously, I'm a little confused

What I would like to do is pretty simple (I thought)
  1. Main program calls a function that creates a GUI
  2. Have MAIN program wait until function finishes execution (of course)
  3. Have GUI function prompt user for a numeric value
  4. Have GUI function prompt user to select a date
  5. Display both values inside the GUI function
  6. Return the numeric value to the main program


WHAT AM I DOING WRONG?? WHY WON'T THE FOLLOWING WORK?????

Code:
;------------ Get Medical Record # ------------------------
ReturnValue = ""
MRN := GetMRN(ReturnValue)
MsgBox, Here is the return value of the function "MRN" %MRN%
ExitApp

;---------------- Function GetMRN ---------------
GetMRN(ReturnValue)
{
Static MRN
Static MyCalendar
Gui, Add, Text, ,Enter the Medical Record Number Here
Gui, Add, Button, Default, OK
Gui, Add, Edit, vMRN 

Gui, Add, MonthCal, vMyCalendar
Gui, Add, Button, Default, OK
Gui, Add, Edit, MyCalendar 

Gui, Show
; I've disabled the "RETURN" line below because it causes the entire function to RETURN prematurely
; return

ButtonOK:
Gui, Submit, NoHide 
; Now, instead of waiting for user to click OK, the following line executes immediately
MsgBox The "ButtonOK" GUI subroutine reports that you entered "%MRN%
ReturnValue = %MRN%
GUI, Destroy

MsgBox The "ButtonOK" GUI subroutine for Calendar reports a selected date of "%MyCalendar%
return %ReturnValue%
}
Back to top
View user's profile Send private message
daniel2



Joined: 23 Jul 2007
Posts: 47

PostPosted: Mon Jul 30, 2007 3:02 am    Post subject: Reply with quote

I believe your trying to make your Gui function Modal; similar to the way Msgbox & InputBox work. Although there isn't a built in method to accomplish this.. there are numerous workarounds.
Code:
MsgBox, % "Result 1 = " MyGuiFunction()
MsgBox, % "Result 2 = " MyGuiFunction()
ExitApp

MyGuiFunction()  {
  Global MyEdit
  Gui, +LastFound
  GuiHWND := WinExist()           ;--get handle to this gui..

  Gui, Add , Text  ,        , Enter value
  Gui, Add , Edit  , vMyEdit,
  Gui, Add , Button, Default, OK
  Gui, Show

  WinWaitClose, ahk_id %GuiHWND%  ;--waiting for gui to close
  return ReturnCode               ;--returning value
;-------
ButtonOK:
  GuiControlGet, ReturnCode, , MyEdit
  Gui, Destroy
return
;-------
GuiEscape:
GuiClose:
  ReturnCode = -1
  Gui, Destroy
return
}
Back to top
View user's profile Send private message
SxFtSxInch



Joined: 29 Jul 2007
Posts: 2

PostPosted: Thu Aug 02, 2007 3:01 am    Post subject: Help making GUI work inside a function Reply with quote

daniel2 wrote:
I believe your trying to make your Gui function Modal; similar to the way Msgbox & InputBox work. Although there isn't a built in method to accomplish this.. there are numerous workarounds.
Code:
MsgBox, % "Result 1 = " MyGuiFunction()
MsgBox, % "Result 2 = " MyGuiFunction()
ExitApp

MyGuiFunction()  {
  Global MyEdit
  Gui, +LastFound
  GuiHWND := WinExist()           ;--get handle to this gui..

  Gui, Add , Text  ,        , Enter value
  Gui, Add , Edit  , vMyEdit,
  Gui, Add , Button, Default, OK
  Gui, Show

  WinWaitClose, ahk_id %GuiHWND%  ;--waiting for gui to close
  return ReturnCode               ;--returning value
;-------
ButtonOK:
  GuiControlGet, ReturnCode, , MyEdit
  Gui, Destroy
return
;-------
GuiEscape:
GuiClose:
  ReturnCode = -1
  Gui, Destroy
return
}



Thank you ever so much for replying. I appreciate the time you spent crafting an example, however, I remain just as confused as before.

Perhaps it would have been better if you (or someone) were to explain how MY CODE was wrong. While there are numerous "tips" in YOUR code that I am eager to learn, your code example is so very dissimilar from mine that I'm having difficulty understanding its relevance -- and more importantly -- how it can be functionally implemented in MY code.

What MY code attempts (however naïvely) is to call a function that creates two separate GUI boxes (one for numeric input and another for a date value) and then to return the user's input to each of these GUI's back to the calling routine.

Please tell me how MY code is in error.
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2485

PostPosted: Thu Aug 02, 2007 5:07 am    Post subject: Reply with quote

The main problem in your original code (although you may run into other issues later when/if adding to a larger script) is that the Gui, Show command does not wait for user actions before continuing to process the next line of code. In other words, creating a Gui window within a function does not create a special instance of a Gui window that will cause the function to wait until the Gui window has been destroyed before returning. In this case, processing continues on the next line by falling into the ButtonOk: label and continues processing from there until it hits a Return line (or the end of the function). In a function, any Return line will cause the function to return and continue processing at the next line after the function call. Although the ButtonOk: label has been added within the function, it has not been made unique to the function (try creating another ButtonOk: label somewhere else in the script) and will not prevent the function from returning if a return line is encountered before processing the code for the label. I hope this info helps a bit Smile .
Back to top
View user's profile Send private message Visit poster's website
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Thu Aug 02, 2007 6:11 pm    Post subject: Reply with quote

@ Daniel2

Excellent piece of coding...

@ SxFtSxInch

Quite the reverse. You need to tailor your code to match the template provided by Daniel2, which provides very nicely, the solution you requested.

Regards Dave.
_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2485

PostPosted: Fri Aug 03, 2007 12:04 am    Post subject: Reply with quote

Dippy46 wrote:
@ SxFtSxInch

Quite the reverse. You need to tailor your code to match the template provided by Daniel2, which provides very nicely, the solution you requested.
Not a bad suggestion... but I'm guessing that that would probably tend to take the fun and purpose out of trying to learn how... Wink
Back to top
View user's profile Send private message Visit poster's website
schlice



Joined: 02 Apr 2010
Posts: 2

PostPosted: Sat Dec 03, 2011 5:08 pm    Post subject: Still Useful Reply with quote

I just wanted to let you all know that this post is still useful. I ran into a similar problem as the OP did (I was just trying to pop a Help screen up) and rather than using a MsgBox, which I probably should have, I made another Gui window.

Thanks for the smart coding.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group