I thought I would add my own twopennyworth to this thread by making a few changes that seemed useful:
1. Width and height parameters added. These are not the dimensions of the overall input box, but those of the text box containing the prompt string. If the width and height are both specified as zero, they default to 128 x 20, which is about the smallest useful size.
2. Instead of just being able to set the password flag, any #PB_String_xxx flag can now be set.
For example: #PB_String_UpperCase
3. The OK button has been moved further to the left (because I preferred the appearance of the input box to be symmetrical).
4. The input box no longer beeps each second when it loses the input focus (because the beeps annoyed me).
5. All gadget numbers are now generated by #PB_Any instead of being hardcoded as arbitrary numbers such as 997.
6. All variables are now declared in order to be EnableExplicit compliant.
Code: Select all
Procedure.s InputBox(txtw, txth, title$, prompt$, def$="", flags=0)
; A version of InputRequester() by BackupUser
; Modified by AKJ 18-Oct-06
; Downloaded: www.purebasic.fr/english/viewtopic.php?t=2412
; textw and txth are the outer dimensions of the prompt text gadget
; Available flags are:
; #PB_String_{BorderLess LowerCase Numeric Password ReadOnly Uppercase}
; Features:
; (1) Modal (locks the calling window until done)
; (2) Prompt area is larger
; (3) Prompt area supports multi-line text with Chr(13)
; (4) Cancel button in addition to OK button
; (5) Returns the default string (def$) if the Cancel button is pressed
; (6) Returns "" if an error occurs
; (7) Standard Windows look-And-feel
; (8) Plays the "question" prompt sound
; (9) Works with or without a calling window
;(10) Emulates the Visual Basic InputBox() command
;(11) Supports all #PB_String_xxx flags
Protected winBox, txtPrompt, strInput, butOK, butCancel ; Gadgets
Protected box, ev, id, ret, esc, a, thread1, thread2, text$=""
If txtw<128 : txtw=128 : EndIf ; To show buttons correctly
If txth<20 : txth = 20 : EndIf ; Height of a normal text line
winBox=OpenWindow(#PB_Any,0,0,txtw+13,txth+20+23+8*4,title$,#PB_Window_ScreenCentered)
box=WindowID(winBox)
If winBox And CreateGadgetList(box)
txtPrompt=TextGadget(#PB_Any,6,8,txtw,txth,prompt$)
If flags&#PB_String_Password=0 : flags|#ES_MULTILINE : EndIf
strInput=StringGadget(#PB_Any,6,txth+8*2,txtw,20,def$,flags|#ES_AUTOVSCROLL)
butOK=ButtonGadget(#PB_Any,6,txth+20+8*3,60,23,"OK") ; Set x=txtw-122 for button to be on the right
butCancel=ButtonGadget(#PB_Any,txtw-54,txth+20+8*3,60,23,"Cancel")
SendMessage_(GadgetID(strInput),#EM_SETSEL,0,Len(def$))
GetAsyncKeyState_(#VK_RETURN) : GetAsyncKeyState_(#VK_ESCAPE)
StickyWindow(winBox,#True) : SetForegroundWindow_(box) : SetActiveGadget(strInput) : MessageBeep_(#MB_ICONQUESTION)
Repeat
ev=WaitWindowEvent(1) : id=EventGadget() : ret=GetAsyncKeyState_(#VK_RETURN) : esc=GetAsyncKeyState_(#VK_ESCAPE)
a=GetForegroundWindow_()
If a<>box
thread1=GetWindowThreadProcessId_(@a,0) : thread2=GetWindowThreadProcessId_(@box,0)
If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
SetForegroundWindow_(box) : Sleep_(1)
If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
SetActiveGadget(strInput); !!! : MessageBeep_(#MB_ICONQUESTION)
EndIf
Until (ev=#PB_Event_Gadget And (id=butOK Or id=butCancel)) Or ret<>0 Or esc<>0 Or ev=#PB_Event_CloseWindow
If id=butOK Or ret<>0 : text$=GetGadgetText(strInput) : Else : text$=def$ : EndIf
CloseWindow(winBox)
EndIf
ProcedureReturn text$
EndProcedure
; Example using BackupUser's dimensions for the prompt text gadget
Debug InputBox(487, 70, "Test", "Enter something:", "default", #PB_String_UpperCase)