how to clear all StringGadgets except Read Only gadget.

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 297
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

how to clear all StringGadgets except Read Only gadget.

Post by Columbo »

I have a number of string gadgets in a PanelGadget. One of these StringGadgets is “Read Only”. What I am trying to do is,… when a user goes to this panel, all of the StringGadgets are cleared except for the “Read Only” gadget. To do this, I initially tried a loop like this:

Code: Select all

Procedure clearAddMemberGadgets()  
  For x = #addFirstName To #addDeptNotes
    SetGadgetText(x,"")    
  Next x 
EndProcedure
The problem here is that it also clears the “Read Only” gadget which I want to leave as is.

Then I tried doing a loop from the first StringGadget to the StringGadget immediately prior to the “Read Only” gadget. Then another loop from the StringGadget immediately following the “Read Only” gadget, essentially skipping over the “Read Only” gadget.

Code: Select all

Procedure clearAddMemberGadgets()  
  For x = #addFirstName To #addDeptNotes
    SetGadgetText(x,"")    
  Next x

For x = #addPostalCode To #addDeptNotes
    SetGadgetText(x,"")    
  Next x 
EndProcedure

For some reason the “Read Only” gadget cleared along with the other StringGadgets although I don’t understand why?

I tried using ClearGadgetItems but again the “Read Only” gadget clears as well.

Any suggestions on how this can be done?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 297
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: how to clear all StringGadgets except Read Only gadget.

Post by Columbo »

SOLVED! Please ignore this post. I found the error.

Thanks
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
BarryG
Addict
Addict
Posts: 1709
Joined: Thu Apr 18, 2019 8:17 am

Re: how to clear all StringGadgets except Read Only gadget.

Post by BarryG »

Columbo wrote: Fri Jun 11, 2021 4:42 pmthe “Read Only” gadget cleared along with the other StringGadgets although I don’t understand why?
Because a read-only StringGadget means it's read-only to prevent the user typing in it; not for the app changing it.

I know you said you solved it, but here's a Windows way if someone later finds this topic and is interested:

Code: Select all

#first=1
#second=2
#third=3
#fourth=4
#last=5

#button=6

OpenWindow(0,200,200,300,175,"test",#PB_Window_SystemMenu)

StringGadget(#first,10,10,280,20,"one")
StringGadget(#second,10,35,280,20,"two")
StringGadget(#third,10,60,280,20,"three",#PB_String_ReadOnly)
StringGadget(#fourth,10,85,280,20,"four")
StringGadget(#last,10,115,280,20,"five")

ButtonGadget(#button,10,140,280,25,"Clear all except 'three'")

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget And EventGadget()=#button
    For gad=#first To #last
      If GetWindowLongPtr_(GadgetID(gad),#GWL_STYLE) & #ES_READONLY = 0
        SetGadgetText(gad,"")
      EndIf
    Next
  EndIf
Until ev=#PB_Event_CloseWindow
User avatar
Shardik
Addict
Addict
Posts: 1840
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: how to clear all StringGadgets except Read Only gadget.

Post by Shardik »

I have modified BarryG's nice example to work cross-platform:

Code: Select all

#first=1
#second=2
#third=3
#fourth=4
#last=5

#button=6

OpenWindow(0,200,200,300,175,"test",#PB_Window_SystemMenu)

StringGadget(#first,10,10,280,20,"one")
StringGadget(#second,10,35,280,20,"two")
StringGadget(#third,10,60,280,20,"three",#PB_String_ReadOnly)
StringGadget(#fourth,10,85,280,20,"four")
StringGadget(#last,10,115,280,20,"five")

ButtonGadget(#button,10,140,280,25,"Clear all except 'three'")

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget And EventGadget()=#button
    For gad=#first To #last
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Linux
          If gtk_editable_get_editable_(GadgetID(gad))
            SetGadgetText(gad, "")
          EndIf
        CompilerCase #PB_OS_MacOS
          If CocoaMessage(0, GadgetID(gad), "isEditable")
            SetGadgetText(gad, "")
          EndIf
        CompilerCase #PB_OS_Windows
          If GetWindowLongPtr_(GadgetID(gad), #GWL_STYLE) & #ES_READONLY = 0
            SetGadgetText(gad, "")
          EndIf
      CompilerEndSelect
    Next
  EndIf
Until ev=#PB_Event_CloseWindow
BarryG
Addict
Addict
Posts: 1709
Joined: Thu Apr 18, 2019 8:17 am

Re: how to clear all StringGadgets except Read Only gadget.

Post by BarryG »

A shorter cross-platform way if the gadgets don't already use SetGadgetData():

Code: Select all

#first=1
#second=2
#third=3
#fourth=4
#last=5

#button=6

OpenWindow(0,200,200,300,175,"test",#PB_Window_SystemMenu)

StringGadget(#first,10,10,280,20,"one")
StringGadget(#second,10,35,280,20,"two")

StringGadget(#third,10,60,280,20,"three",#PB_String_ReadOnly)
SetGadgetData(#third,#PB_String_ReadOnly)

StringGadget(#fourth,10,85,280,20,"four")
StringGadget(#last,10,115,280,20,"five")

ButtonGadget(#button,10,140,280,25,"Clear all except 'three'")

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget And EventGadget()=#button
    For gad=#first To #last
      If GetGadgetData(gad)<>#PB_String_ReadOnly
        SetGadgetText(gad,"")
      EndIf
    Next
  EndIf
Until ev=#PB_Event_CloseWindow
User avatar
mk-soft
Addict
Addict
Posts: 3191
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how to clear all StringGadgets except Read Only gadget.

Post by mk-soft »

@Shardik,

Nice ;)
I usually forget the subsystem "QT" even with cross-platform.

Code: Select all

;-TOP

Procedure IsGadgetEditable(Gadget)
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      CompilerIf Subsystem("QT")
        ProcedureReturn Bool(qtscript("gadget(" + Str(Gadget) + ").readOnly") <> "true")
        
      CompilerElse
        ProcedureReturn gtk_editable_get_editable_(GadgetID(Gadget))
      CompilerEndIf
      
    CompilerCase #PB_OS_MacOS
      ProcedureReturn CocoaMessage(0, GadgetID(Gadget), "isEditable")
      
    CompilerCase #PB_OS_Windows
      ProcedureReturn Bool(GetWindowLongPtr_(GadgetID(gadg), #GWL_STYLE) & #ES_READONLY = 0)
      
  CompilerEndSelect
  
EndProcedure

; ****

#first=1
#second=2
#third=3
#fourth=4
#last=5

#button=6

OpenWindow(0,200,200,300,175,"test",#PB_Window_SystemMenu)

StringGadget(#first,10,10,280,20,"one")
StringGadget(#second,10,35,280,20,"two")
StringGadget(#third,10,60,280,20,"three",#PB_String_ReadOnly)
StringGadget(#fourth,10,85,280,20,"four")
StringGadget(#last,10,115,280,20,"five")

ButtonGadget(#button,10,140,280,25,"Clear all except 'three'")

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Gadget And EventGadget()=#button
    For gad=#first To #last
      If IsGadgetEditable(gad)
        SetGadgetText(gad, "")
      EndIf  
    Next
  EndIf
Until ev=#PB_Event_CloseWindow
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4094
Joined: Sun Apr 12, 2009 6:27 am

Re: how to clear all StringGadgets except Read Only gadget.

Post by RASHAD »

Just for fun

Code: Select all

#first=1
#second=2
#third=3
#fourth=4
#last=5

#button=6

OpenWindow(0,200,200,300,175,"test",#PB_Window_SystemMenu)
;ContainerGadget(#PB_Any,0,0,300,130)
StringGadget(#first,10,10,280,20,"one")
StringGadget(#second,10,35,280,20,"two")
StringGadget(#third,10,60,280,20,"three",#PB_String_ReadOnly)
StringGadget(#fourth,10,85,280,20,"four")
StringGadget(#last,10,115,280,20,"five")
;CloseGadgetList()
ButtonGadget(#button,10,140,280,25,"Clear all except 'three'")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #button
          For gad=#first To #last
            If GadgetType(gad) = #PB_GadgetType_String
              StartDrawing(WindowOutput(0))
              result = Point(GadgetX(gad)+2,GadgetY(gad)+2)
              StopDrawing()
              If result = $FFFFFF
                SetGadgetText(gad,"")
              EndIf
            EndIf
          Next
      EndSelect
  EndSelect
Until Quit = 1
Egypt my love
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 297
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: how to clear all StringGadgets except Read Only gadget.

Post by Columbo »

Thanks to all. I'm sure that these will be a help to others in the future.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Post Reply