InputBox() (Updated Again for v2.4)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

InputBox() (Updated Again for v2.4)

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by wayne1.

Code: Select all

;simple input box
;caution, not modal, but will stay above all other windows 
;message=prompt
;caption=input box title
;defaultString=a suggested response
; 
Procedure.s InputBox(message.s,caption.s,defaultString.s) 
  
  Protected xP=220, yP=200, xW=365, yH=140
  Protected inpBoxHnd, answer.s = "", OKorCan.b, EventID
  #TOP_MOST=-1
  
  If OpenWindow(0, xP, yP, xW, yH, caption, #PB_Window_SystemMenu )
    
    ButtonGadget(0, 290, 10, 60, 22, "OK") 
    ButtonGadget(1, 290, 35, 60, 22, "Cancel") 
    TextGadget(2, 10, 10, 275, 25, message)
    StringGadget(3, 10, 80, 340, 20, defaultString)
    SetActiveGadget(3)
    inpBoxHnd= FindWindow_(0, caption)
    SetWindowPos_(inpBoxHnd,#TOP_MOST,xP,yP,xW,yH,0);set InputBox to TOP_MOST (above all other windows)
    
    
    Repeat
      EventID = WaitWindowEvent()
      
      If  EventID = #PB_Event_CloseWindow
        answer=""
        Goto exit
      EndIf
      
      
      If EventID = #PB_Event_Gadget
        
        Select EventGadget()
          Case 0 
            answer=GetGadgetText(3)
            EventID = #PB_Event_CloseWindow
            OKorCan=1
            
          Case 1   
            answer=""
            EventID = #PB_Event_CloseWindow
            OKorCan=1
            
        EndSelect
        
        If OKorCan 
          Goto exit
        EndIf
        
        
      EndIf
      
    ForEver
    
    exit:
    
  EndIf
  CloseWindow(0) 
  ProcedureReturn answer
  
EndProcedure



Repeat
  a$=InputBox("Enter Your Name","Input Box","")
  If a$ <> ""
    MessageRequester("Your Name",a$,0)
  Else
    MessageRequester("","You did not enter anything or you pressed cancel or exit.",0)
  EndIf
Until a$ <> ""

End
Edited by - wayne1 on 10 August 2001 03:17:16
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hm, I think it is not nice to jump our of a running loop without ending it...

Code: Select all

...
  If OKorCan 
    Goto exit
    EndIf
  EndIf
ForEver
exit:
...

Only as a hint. I don't know the stacksize on windows but it definitly does not work for ever or does windows automatical clear the stack? Hm....

Mike

Tranquilizer/ Secretly!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
Hm, I think it is not nice to jump our of a running loop without ending it...
That's a good reaction. In that case, the jum is correct as Repeat/Forever doesn't pu anything on the stack. I will add an 'exit' command to exit cleanly from a loop.

Fred - AlphaSND
Post Reply