AutoHotkey Community

It is currently May 25th, 2012, 1:43 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: July 30th, 2007, 12:25 am 
Offline

Joined: July 29th, 2007, 9:40 pm
Posts: 2
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%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 30th, 2007, 4:02 am 
Offline

Joined: July 23rd, 2007, 3:43 am
Posts: 47
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
}


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 2nd, 2007, 4:01 am 
Offline

Joined: July 29th, 2007, 9:40 pm
Posts: 2
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2007, 6:07 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2541
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 :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2007, 7:11 pm 
Offline

Joined: July 6th, 2004, 10:07 am
Posts: 171
Location: Manchester, England.
@ 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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 3rd, 2007, 1:04 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2541
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... ;)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Still Useful
PostPosted: December 3rd, 2011, 6:08 pm 
Offline

Joined: April 2nd, 2010, 4:14 am
Posts: 3
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], CodeKiller, engunneer, Prankie, Pulover, rbrtryn, Tegno and 24 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group