PureBasic Forum
http://forums.purebasic.com/english/

[solved]What is wrong with this toggle password fields code?
http://forums.purebasic.com/english/viewtopic.php?f=13&t=75987
Page 1 of 1

Author:  doctorized [ Fri Sep 25, 2020 8:51 pm ]
Post subject:  [solved]What is wrong with this toggle password fields code?

I have the following code to change a password in an app. The problem is that pressing the toggle buttons (to show/hide the password) their behavior is not the one it should be. Any ideas why?
Code:
UsePNGImageDecoder()
Enumeration
   #PasswordWindow
   #Pwd_txt_passwordCurrent
   #Pwd_txt_passwordNew
   #Pwd_txt_passwordNew2
   #Pwd_str_Current
   #Pwd_str_passwordNew
   #Pwd_str_passwordNew2
   #Pwd_frm
   #Pwd_btn_show_pass_current
   #Pwd_btn_show_pass_new
   #Pwd_btn_save
   #Pwd_btn_return
EndEnumeration
Global ShowPassIcon = CatchImage(#PB_Any, ?ShowPassStart, ?ShowPassEnd - ?ShowPassStart)
Global HidePassIcon = CatchImage(#PB_Any, ?HidePassStart, ?HidePassEnd - ?HidePassStart)

Procedure TogglePassword(Gdt.l, GdtToggle.l, ChangeToggle=0)
   Protected tmppass.s, tmpx.l, tmpy.l, tmpw.l, tmph.l
   tmppass.s = GetGadgetText(Gdt)
   tmpx = GadgetX(Gdt)
   tmpy = GadgetY(Gdt)
   tmpw = GadgetWidth(Gdt)
   tmph = GadgetHeight(Gdt)
   If ChangeToggle > 0
      tmpx2 = GadgetX(GdtToggle)
      tmpy2 = GadgetY(GdtToggle)
      tmpw2 = GadgetWidth(GdtToggle)
      tmph2 = GadgetHeight(GdtToggle)
   EndIf
   FreeGadget(Gdt)
   Debug GetGadgetState(GdtToggle)
   If GetGadgetState(GdtToggle) = 0
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass, #PB_String_Password)
      If ChangeToggle > 0
         ButtonImageGadget(GdtToggle, tmpx2, tmpy2, tmpw2, tmph2,ImageID(ShowPassIcon),#PB_Button_Toggle)
         SetGadgetState(GdtToggle,~GetGadgetState(GdtToggle))
      EndIf
   Else
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass)
      If ChangeToggle > 0
         ButtonImageGadget(GdtToggle, tmpx2, tmpy2, tmpw2, tmph2,ImageID(HidePassIcon),#PB_Button_Toggle)
         SetGadgetState(GdtToggle,~GetGadgetState(GdtToggle))
         
      EndIf
   EndIf
   Debug GetGadgetState(GdtToggle)
   Debug "******"
   ;SetGadgetText(Gdt, tmppass)
EndProcedure


If   OpenWindow(#PasswordWindow,0,0,480,180,"change password", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
   FrameGadget(#Pwd_frm,10,5,460,135,"change current password")
   TextGadget(#Pwd_txt_passwordCurrent, 15, 25, 220, 21, "give current password")
   StringGadget(#Pwd_str_Current, 260, 25, 180, 22, "current", #PB_String_Password)
   TextGadget(#Pwd_txt_passwordNew, 15, 55,230, 21, "give new password")
   StringGadget(#Pwd_str_passwordNew, 260, 55, 180, 22, "pass 1", #PB_String_Password)
   TextGadget(#Pwd_txt_passwordNew2, 15, 85, 240, 21, "confirm new password")
   StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, "pass 2", #PB_String_Password)
   
   ButtonImageGadget(#Pwd_btn_show_pass_current, 440, 24, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
   ButtonImageGadget(#Pwd_btn_show_pass_new, 440, 54, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
   ButtonGadget(#Pwd_btn_return, (WindowWidth(#PasswordWindow)-80)/2,WindowHeight(#PasswordWindow)-35 , 80, 25, "return")
   
   Repeat
    Event = WaitWindowEvent()

    Select Event
          Case #PB_Event_CloseWindow
             Quit = 1
          Case #PB_Event_Gadget
         Select EventGadget()
             Case #Pwd_btn_show_pass_current
               TogglePassword(#Pwd_str_Current,#Pwd_btn_show_pass_current,1)
            Case #Pwd_btn_show_pass_new
               TogglePassword(#Pwd_str_passwordNew,#Pwd_btn_show_pass_new,1)
               TogglePassword(#Pwd_str_passwordNew2,#Pwd_btn_show_pass_new)   
         EndSelect
    EndSelect

  Until Quit = 1
 
EndIf

DataSection
   ShowPassStart:
      IncludeBinary "show.png"
   ShowPassEnd:
   HidePassStart:
      IncludeBinary "hide.png"
      HidePassEnd:
EndDataSection
End
   

The images are here:
http://users.sch.gr/arahiotis/other/hide.png
http://users.sch.gr/arahiotis/other/show.png

Author:  RASHAD [ Fri Sep 25, 2020 9:28 pm ]
Post subject:  Re: What is wrong with this toggle password fields code?

Did not test it but I hope it do the job
Code:
Procedure TogglePassword(Gdt.l, GdtToggle.l, ChangeToggle=0)
   Protected tmppass.s, tmpx.l, tmpy.l, tmpw.l, tmph.l
   tmppass.s = GetGadgetText(Gdt)
   tmpx = GadgetX(Gdt)
   tmpy = GadgetY(Gdt)
   tmpw = GadgetWidth(Gdt)
   tmph = GadgetHeight(Gdt)
   If ChangeToggle > 0
      tmpx2 = GadgetX(GdtToggle)
      tmpy2 = GadgetY(GdtToggle)
      tmpw2 = GadgetWidth(GdtToggle)
      tmph2 = GadgetHeight(GdtToggle)
   EndIf
   FreeGadget(Gdt)
   ;Debug GetGadgetState(GdtToggle)
   If GetGadgetState(GdtToggle) = 0
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass, #PB_String_Password)
      ResizeGadget(GdtToggle,tmpx2, tmpy2, tmpw2, tmph2)
      SetGadgetAttribute(GdtToggle,#PB_Button_Image,ImageID(ShowPassIcon))
      ;If ChangeToggle > 0
      ;ButtonImageGadget(GdtToggle, tmpx2, tmpy2, tmpw2, tmph2,ImageID(ShowPassIcon),#PB_Button_Toggle)
      ;SetGadgetState(GdtToggle,~GetGadgetState(GdtToggle))
      ;EndIf
   Else
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass)
      ResizeGadget(GdtToggle,tmpx2, tmpy2, tmpw2, tmph2)
      SetGadgetAttribute(GdtToggle,#PB_Button_Image,ImageID(HidePassIcon)))
      ;If ChangeToggle > 0
      ;ButtonImageGadget(GdtToggle, tmpx2, tmpy2, tmpw2, tmph2,ImageID(HidePassIcon),#PB_Button_Toggle)
      ;SetGadgetState(GdtToggle,~GetGadgetState(GdtToggle))         
      ;EndIf
   EndIf
   ;Debug GetGadgetState(GdtToggle)
   ;Debug "******"
   ;SetGadgetText(Gdt, tmppass)
EndProcedure

Author:  infratec [ Fri Sep 25, 2020 9:32 pm ]
Post subject:  Re: What is wrong with this toggle password fields code?

My 'simpler' version: (and not working as it should :oops: )
Code:
UsePNGImageDecoder()
Enumeration
  #PasswordWindow
  #Pwd_txt_passwordCurrent
  #Pwd_txt_passwordNew
  #Pwd_txt_passwordNew2
  #Pwd_str_Current
  #Pwd_str_passwordNew
  #Pwd_str_passwordNew2
  #Pwd_frm
  #Pwd_btn_show_pass_current
  #Pwd_btn_show_pass_new
  #Pwd_btn_save
  #Pwd_btn_return
EndEnumeration

Global ShowPassIcon = CatchImage(#PB_Any, ?ShowPassStart, ?ShowPassEnd - ?ShowPassStart)
Global HidePassIcon = CatchImage(#PB_Any, ?HidePassStart, ?HidePassEnd - ?HidePassStart)


Procedure TogglePassword(Gdt.i, GdtToggle.i)
 
  Protected Help$
 
 
  If GetGadgetState(GdtToggle)
    SetGadgetText(Gdt, PeekS(GetGadgetData(Gdt)))
  Else
    Help$ = Space(Len(PeekS(GetGadgetData(Gdt))))
    Help$ = ReplaceString(Help$, " ", Chr($25CF))
    SetGadgetText(Gdt, Help$)
  EndIf
 
EndProcedure




If   OpenWindow(#PasswordWindow,0,0,480,180,"change password", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  FrameGadget(#Pwd_frm,10,5,460,135,"change current password")
  TextGadget(#Pwd_txt_passwordCurrent, 15, 25, 220, 21, "give current password")
  Current$ = "current"
  StringGadget(#Pwd_str_Current, 260, 25, 180, 22, "")
  SetGadgetData(#Pwd_str_Current, @Current$)
 
  TextGadget(#Pwd_txt_passwordNew, 15, 55,230, 21, "give new password")
  PasswordNew$ = "pass 1"
  StringGadget(#Pwd_str_passwordNew, 260, 55, 180, 22, PasswordNew$)
  SetGadgetData(#Pwd_str_passwordNew, @PasswordNew$)
 
  TextGadget(#Pwd_txt_passwordNew2, 15, 85, 240, 21, "confirm new password")
  PasswordNew2$ = "pass 2"
  StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, "")
  SetGadgetData(#Pwd_str_passwordNew2, @PasswordNew2$)

  ButtonImageGadget(#Pwd_btn_show_pass_current, 440, 24, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
  SetGadgetAttribute(#Pwd_btn_show_pass_current, #PB_Button_PressedImage, ImageID(HidePassIcon))
  ButtonImageGadget(#Pwd_btn_show_pass_new, 440, 54, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
  SetGadgetAttribute(#Pwd_btn_show_pass_new, #PB_Button_PressedImage, ImageID(HidePassIcon))
  ButtonGadget(#Pwd_btn_return, (WindowWidth(#PasswordWindow)-80)/2,WindowHeight(#PasswordWindow)-35 , 80, 25, "return")
 
  TogglePassword(#Pwd_str_Current, #Pwd_btn_show_pass_current)
  TogglePassword(#Pwd_str_passwordNew, #Pwd_btn_show_pass_new)
  TogglePassword(#Pwd_str_passwordNew2, #Pwd_btn_show_pass_new)
 
  Repeat
    Event = WaitWindowEvent()
   
    Select Event
      Case #PB_Event_CloseWindow
        Quit = #True
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Pwd_btn_show_pass_current
            TogglePassword(#Pwd_str_Current, #Pwd_btn_show_pass_current)
          Case #Pwd_btn_show_pass_new
            TogglePassword(#Pwd_str_passwordNew, #Pwd_btn_show_pass_new)
            TogglePassword(#Pwd_str_passwordNew2, #Pwd_btn_show_pass_new)
        EndSelect
    EndSelect
   
  Until Quit
 
EndIf

DataSection
  ShowPassStart:
  IncludeBinary "show.png"
  ShowPassEnd:
  HidePassStart:
  IncludeBinary "hide.png"
  HidePassEnd:
EndDataSection
End

Author:  infratec [ Fri Sep 25, 2020 9:52 pm ]
Post subject:  Re: What is wrong with this toggle password fields code?

Second try :wink: (but with Windows API)
Code:
EnableExplicit

Enumeration
  #PasswordWindow
  #Pwd_txt_passwordCurrent
  #Pwd_txt_passwordNew
  #Pwd_txt_passwordNew2
  #Pwd_str_Current
  #Pwd_str_passwordNew
  #Pwd_str_passwordNew2
  #Pwd_frm
  #Pwd_btn_show_pass_current
  #Pwd_btn_show_pass_new
  #Pwd_btn_save
  #Pwd_btn_return
EndEnumeration



Procedure TogglePassword(Gdt.i, GdtToggle.i)
 
  If GetGadgetState(GdtToggle)
    SendMessage_(GadgetID(Gdt), #EM_SETPASSWORDCHAR, 0, 0)
    ResizeGadget(Gdt, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
  Else
    SendMessage_(GadgetID(Gdt), #EM_SETPASSWORDCHAR, $25CF, 0)
    ResizeGadget(Gdt, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
  EndIf
 
EndProcedure



Define.i ShowPassIcon, HidePassIcon, Event, Quit

UsePNGImageDecoder()

ShowPassIcon = CatchImage(#PB_Any, ?ShowPassStart, ?ShowPassEnd - ?ShowPassStart)
HidePassIcon = CatchImage(#PB_Any, ?HidePassStart, ?HidePassEnd - ?HidePassStart)

If OpenWindow(#PasswordWindow,0,0,480,180,"change password", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
 
  FrameGadget(#Pwd_frm,10,5,460,135,"change current password")
 
  TextGadget(#Pwd_txt_passwordCurrent, 15, 25, 220, 21, "give current password")
  StringGadget(#Pwd_str_Current, 260, 25, 180, 22, "current", #PB_String_Password)

  TextGadget(#Pwd_txt_passwordNew, 15, 55,230, 21, "give new password")
  StringGadget(#Pwd_str_passwordNew, 260, 55, 180, 22, "pass 1", #PB_String_Password)
 
  TextGadget(#Pwd_txt_passwordNew2, 15, 85, 240, 21, "confirm new password")
  StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, "pass 2", #PB_String_Password)

  ButtonImageGadget(#Pwd_btn_show_pass_current, 440, 24, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
  SetGadgetAttribute(#Pwd_btn_show_pass_current, #PB_Button_PressedImage, ImageID(HidePassIcon))
 
  ButtonImageGadget(#Pwd_btn_show_pass_new, 440, 54, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
  SetGadgetAttribute(#Pwd_btn_show_pass_new, #PB_Button_PressedImage, ImageID(HidePassIcon))
 
  ButtonGadget(#Pwd_btn_return, (WindowWidth(#PasswordWindow)-80)/2,WindowHeight(#PasswordWindow)-35 , 80, 25, "return")
 
 
  Repeat
    Event = WaitWindowEvent()
   
    Select Event
      Case #PB_Event_CloseWindow
        Quit = #True
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Pwd_btn_show_pass_current
            TogglePassword(#Pwd_str_Current, #Pwd_btn_show_pass_current)
          Case #Pwd_btn_show_pass_new
            TogglePassword(#Pwd_str_passwordNew, #Pwd_btn_show_pass_new)
            TogglePassword(#Pwd_str_passwordNew2, #Pwd_btn_show_pass_new)
        EndSelect
    EndSelect
   
  Until Quit
 
EndIf

DataSection
  ShowPassStart:
  IncludeBinary "show.png"
  ShowPassEnd:
  HidePassStart:
  IncludeBinary "hide.png"
  HidePassEnd:
EndDataSection

End

Author:  RASHAD [ Fri Sep 25, 2020 11:53 pm ]
Post subject:  Re: What is wrong with this toggle password fields code?

The next should do the job as required
Code:
UsePNGImageDecoder()
Enumeration
   #PasswordWindow
   #Pwd_txt_passwordCurrent
   #Pwd_txt_passwordNew
   #Pwd_txt_passwordNew2
   #Pwd_str_Current
   #Pwd_str_passwordNew
   #Pwd_str_passwordNew2
   #Pwd_frm
   #Pwd_btn_show_pass_current
   #Pwd_btn_show_pass_new
   #Pwd_btn_save
   #Pwd_btn_return
EndEnumeration

Global ShowPassIcon = CatchImage(#PB_Any, ?ShowPassStart, ?ShowPassEnd - ?ShowPassStart)
Global HidePassIcon = CatchImage(#PB_Any, ?HidePassStart, ?HidePassEnd - ?HidePassStart)

Procedure TogglePassword(Gdt.l, GdtToggle.l, ChangeToggle=0)
   Protected tmppass.s,tmppass2.s, tmpx.l, tmpy.l, tmpw.l, tmph.l
   tmppass.s = GetGadgetText(Gdt)
   tmppass2.s = GetGadgetText(#Pwd_str_passwordNew2)
   tmpx = GadgetX(Gdt)
   tmpy = GadgetY(Gdt)
   tmpw = GadgetWidth(Gdt)
   tmph = GadgetHeight(Gdt)
   If ChangeToggle > 0
      tmpx2 = GadgetX(GdtToggle)
      tmpy2 = GadgetY(GdtToggle)
      tmpw2 = GadgetWidth(GdtToggle)
      tmph2 = GadgetHeight(GdtToggle)
   EndIf
   FreeGadget(Gdt)
   If GetGadgetState(GdtToggle) = 0
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass, #PB_String_Password)
      ;ResizeGadget(GdtToggle,tmpx2, tmpy2, tmpw2, tmph2)
      SetGadgetAttribute(GdtToggle,#PB_Button_Image,ImageID(ShowPassIcon))
       If GdtToggle = #Pwd_btn_show_pass_new
         StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, tmppass2, #PB_String_Password)
       EndIf
   Else
      StringGadget(Gdt, tmpx, tmpy, tmpw, tmph, tmppass)
      ;ResizeGadget(GdtToggle,tmpx2, tmpy2, tmpw2, tmph2)
      SetGadgetAttribute(GdtToggle,#PB_Button_Image,ImageID(HidePassIcon))
       If GdtToggle = #Pwd_btn_show_pass_new
         tmppass2 = GetGadgetText(#Pwd_str_passwordNew2)
         FreeGadget(#Pwd_str_passwordNew2)
         StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, tmppass2)   
       EndIf
   EndIf
EndProcedure


If   OpenWindow(#PasswordWindow,0,0,480,180,"change password", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
   FrameGadget(#Pwd_frm,10,5,460,135,"change current password")
   TextGadget(#Pwd_txt_passwordCurrent, 15, 25, 220, 21, "give current password")
   StringGadget(#Pwd_str_Current, 260, 25, 180, 22, "current", #PB_String_Password)
   TextGadget(#Pwd_txt_passwordNew, 15, 55,230, 21, "give new password")
   StringGadget(#Pwd_str_passwordNew, 260, 55, 180, 22, "pass 1", #PB_String_Password)
   TextGadget(#Pwd_txt_passwordNew2, 15, 85, 240, 21, "confirm new password")
   StringGadget(#Pwd_str_passwordNew2, 260, 85, 180, 22, "pass 2", #PB_String_Password)
   
   ButtonImageGadget(#Pwd_btn_show_pass_current, 440, 24, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
   ButtonImageGadget(#Pwd_btn_show_pass_new, 440, 54, 26, 24,ImageID(ShowPassIcon) , #PB_Button_Toggle)
   ButtonGadget(#Pwd_btn_return, (WindowWidth(#PasswordWindow)-80)/2,WindowHeight(#PasswordWindow)-35 , 80, 25, "return")
   
   Repeat
    Event = WaitWindowEvent()

    Select Event
          Case #PB_Event_CloseWindow
             Quit = 1
          Case #PB_Event_Gadget
         Select EventGadget()
             Case #Pwd_btn_show_pass_current
               TogglePassword(#Pwd_str_Current,#Pwd_btn_show_pass_current,1)
            Case #Pwd_btn_show_pass_new
               TogglePassword(#Pwd_str_passwordNew,#Pwd_btn_show_pass_new,1) 
         EndSelect
    EndSelect

  Until Quit = 1
 
EndIf

DataSection
   ShowPassStart:
      IncludeBinary "show.png"
   ShowPassEnd:
   HidePassStart:
      IncludeBinary "hide.png"
      HidePassEnd:
EndDataSection
End

Author:  doctorized [ Sat Sep 26, 2020 11:53 am ]
Post subject:  Re: What is wrong with this toggle password fields code?

Thank you very much for your answers! They solved my problem!

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/