Numeric Password…

Share your advanced PureBasic knowledge/code with the community.
User avatar
Piero
Addict
Addict
Posts: 890
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Numeric Password…

Post by Piero »

Code: Select all

Global StringGadgetFlags = #PB_String_Numeric ; set to 0 for normal password
#pwtooltip = "Password Input Field" ; use after creating StringGadget
Procedure ShowHidePass(StringGadget, activate = #True)
   Protected x,y,w,h,s.s
   x=GadgetX(StringGadget)
   y=GadgetY(StringGadget)
   w=GadgetWidth(StringGadget)
   h=GadgetHeight(StringGadget)
   s=GetGadgetText(StringGadget)
   FreeGadget(StringGadget)
   StringGadget(StringGadget,x,y,w,h,s,StringGadgetFlags)
   GadgetToolTip(StringGadget,#pwtooltip)
   If activate: SetActiveGadget(StringGadget) :EndIf
   StringGadgetFlags ! #PB_String_Password
EndProcedure

OpenWindow(0, 0, 0, 250, 90, "Numeric Password")
StringGadget(0, 40, 20, 180, 20, "1234", StringGadgetFlags | #PB_String_Password)
GadgetToolTip(0, #pwtooltip )
CheckBoxGadget(1, 40, 55, 180, 25, "Show Password")
SetActiveGadget(0)
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break
      Case #PB_Event_Gadget
         If EventGadget() = 1
            ShowHidePass(0)
         EndIf
         CompilerIf #PB_Compiler_OS = #PB_OS_Windows
            If EventType()=#PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
               SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
            EndIf
         CompilerEndIf      
   EndSelect
ForEver
Last edited by Piero on Fri Aug 22, 2025 7:03 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Numeric Password…

Post by idle »

Thanks that's handy.
User avatar
Piero
Addict
Addict
Posts: 890
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Numeric Password…

Post by Piero »

idle wrote: Fri Aug 22, 2025 6:40 am Thanks that's handy.
YW :)

PS: I just added a "fix" for windoze ;)
Axolotl
Addict
Addict
Posts: 822
Joined: Wed Dec 31, 2008 3:36 pm

Re: Numeric Password…

Post by Axolotl »

Thanks for sharing.

BTW: On Window I prefer another 'fix' called API constant #EM_SETPASSWORDCHAR :oops:

Code: Select all

Global StringGadgetFlags = #PB_String_Numeric ; set to 0 for normal password
#pwtooltip = "Password Input Field" ; use after creating StringGadget

CompilerIf #PB_Compiler_OS = #PB_OS_Windows

Procedure DisableStringGadgetPassword(Gadget, State) 
  If State 
    SendMessage_(GadgetID(Gadget), #EM_SETPASSWORDCHAR, 0, 0)
  Else 
    SendMessage_(GadgetID(Gadget), #EM_SETPASSWORDCHAR, '●', 0) 
  EndIf 
EndProcedure 

CompilerElse ; #PB_Compiler_OS = #ALL_THE_OTHERS 

Procedure ShowHidePass(StringGadget, activate = #True)
   Protected x,y,w,h,s.s
   x=GadgetX(StringGadget)
   y=GadgetY(StringGadget)
   w=GadgetWidth(StringGadget)
   h=GadgetHeight(StringGadget)
   s=GetGadgetText(StringGadget)
   FreeGadget(StringGadget)
   StringGadget(StringGadget,x,y,w,h,s,StringGadgetFlags)
   GadgetToolTip(StringGadget,#pwtooltip)
   If activate: SetActiveGadget(StringGadget) :EndIf
   StringGadgetFlags ! #PB_String_Password
EndProcedure

CompilerEndIf      

OpenWindow(0, 0, 0, 250, 90, "Numeric Password") 
StringGadget(0, 40, 20, 180, 20, "1234", StringGadgetFlags | #PB_String_Password) ; #PB_String_Numeric  
GadgetToolTip(0, #pwtooltip )
CheckBoxGadget(1, 40, 55, 180, 25, "Show Password")
SetActiveGadget(0)
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break

      Case #PB_Event_Gadget
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        If EventGadget() = 1
          DisableStringGadgetPassword(0, GetGadgetState(1)) 
          SetActiveGadget(0) 
        EndIf 
CompilerElse 
       If EventGadget() = 1
        ShowHidePass(0)
       EndIf
       CompilerIf #PB_Compiler_OS = #PB_OS_Windows
          If EventType()=#PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
            SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
          EndIf
       CompilerEndIf      
CompilerEndIf      
   EndSelect
ForEver
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply