It is currently Sat Nov 28, 2020 8:41 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [solved]What is wrong with this toggle password fields code?
PostPosted: Fri Sep 25, 2020 8:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 755
Location: Athens, Greece
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


Last edited by doctorized on Sat Sep 26, 2020 11:53 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: What is wrong with this toggle password fields code?
PostPosted: Fri Sep 25, 2020 9:28 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3730
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

_________________
Egypt my love


Last edited by RASHAD on Fri Sep 25, 2020 9:37 pm, edited 3 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: What is wrong with this toggle password fields code?
PostPosted: Fri Sep 25, 2020 9:32 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5064
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject: Re: What is wrong with this toggle password fields code?
PostPosted: Fri Sep 25, 2020 9:52 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5064
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject: Re: What is wrong with this toggle password fields code?
PostPosted: Fri Sep 25, 2020 11:53 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3730
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

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: What is wrong with this toggle password fields code?
PostPosted: Sat Sep 26, 2020 11:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 755
Location: Athens, Greece
Thank you very much for your answers! They solved my problem!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: DarkDragon and 35 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye