Page 1 of 1
how to clear all StringGadgets except Read Only gadget.
Posted: Fri Jun 11, 2021 4:42 pm
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?
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Fri Jun 11, 2021 5:07 pm
by Columbo
SOLVED! Please ignore this post. I found the error.
Thanks
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 12:49 am
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
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 8:34 am
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
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 8:40 am
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
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 9:08 am
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
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 12:23 pm
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
Re: how to clear all StringGadgets except Read Only gadget.
Posted: Sat Jun 12, 2021 6:28 pm
by Columbo
Thanks to all. I'm sure that these will be a help to others in the future.