String Gadget - Read Only

Just starting out? Need help? Post your questions and find answers here.
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

String Gadget - Read Only

Post by Daffodil the Duck »

Forgive this question but how do I make a string gadget read only once it is created. I know it can be created as read only but 'read only' is an attribute I would like to turn off and on as determined by the business logic. I know also that I can disable gadgets but that makes the content invisible so is not quite what I am trying to achieve. I am trying to achieve a field where the user can enter text when I let them and read only (but still displaying the field data) otherwise. I'm sure it is simple but I can't seem to find anything in the help text or this forum. Thanks in advance.
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post by HeX0R »

Code: Select all

If OpenWindow(0,0,0,300,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Test") And CreateGadgetList(WindowID(0)) 
  StringGadget(0,8, 10,200,20,"Some Text")
  ButtonGadget(1,8, 40, 90, 20, "Change")
  State.l = 0
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        If EventGadgetID() = 1
          State = 1 - State
          SendMessage_(GadgetID(0), #EM_SETREADONLY, State, 0)
        EndIf
    EndSelect
  ForEver
EndIf
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Thanks HeX0R. That works. It's great. I sense I have a whole world of WINAPI's to explore .... somehow.

I had naively thought that this same command would apply to all gadgets but it seems only to work with string gadgets, which is after all what I asked for.

Code: Select all

#ReadOnly = 1
#Editable = 0
SendMessage_(GadgetID(#StringTopic), #EM_SETREADONLY, #ReadOnly, 0)
SendMessage_(GadgetID(#StringTopic), #EM_SETREADONLY, #Editable, 0)
What do I change if I want this to also work for other gadgets (ComboBox, Checkbox, Editor, Option, Tree, etc)?
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

No real need to use the WinAPI unless you want to.

Code: Select all

If OpenWindow(0,0,0,300,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Test")
  CreateGadgetList(WindowID(0)) 
  StringGadget(0,  8, 10, 200, 20, "Some Text") 
  ButtonGadget(1,  8, 40,  90, 20, "Read Only") 
  ButtonGadget(2,110, 40,  90, 20, "Changeable") 
  
  Repeat 
    Event = WaitWindowEvent()
    Select Event 
      Case #PB_Event_Gadget 
        If EventGadgetID() = 1 
          StringGadget(0,8, 10,200,20,GetGadgetText(0),#PB_String_ReadOnly)
        EndIf
        If EventGadgetID() = 2 
          StringGadget(0,8, 10,200,20,GetGadgetText(0))               
        EndIf 
    EndSelect 
  Until Event = #PB_Event_CloseWindow 
EndIf 
End  
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Thanks Terry. That works well also. I modified it so that I don't have to remember the X and Y coords etc and it works just great.

Code: Select all

StringGadget(0, GadgetX(0), GadgetY(0), GadgetWidth(0), GadgetHeight(0), GetGadgetText(0), #PB_String_ReadOnly) 
I don't know which is best (API or PureBasic). API might be faster (maybe)while on the other hand there does seem something very pure about using only the PureBasic language where possible.

Regarding my followon question, I am reaching the conclusion that most other gadgets are not capable of read only mode but am still looking.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

For combo, checkbox, etc. ReadOnly implies you want to see the settings but not be able to change them... why not use DisableGadget ?
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

:lol: Where was my memory?

DisableGadget() is much simpler and faster.

Code: Select all

If OpenWindow(0,0,0,300,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Test") 
  CreateGadgetList(WindowID(0)) 
  StringGadget(0,  8, 10, 200, 20, "Some Text") 
  ButtonGadget(1,  8, 40,  90, 20, "Read Only") 
  ButtonGadget(2,110, 40,  90, 20, "Changeable") 
  DisableGadget(2,1)
  OptionGadget(3,  8, 70, 150, 20, "Disallow text change?") 
  CheckBoxGadget(4, 8, 100, 100, 20, "Check my box") 
  SetGadgetState(4,1)     
  EditorGadget(5, 8, 130, 286, 64)
  SetGadgetText(5,"Put some text in the editor gadget and see if it can be read.")
       
  Repeat 
    Event = WaitWindowEvent() 
    Select Event 
      Case #PB_Event_Gadget 
        If EventGadgetID() = 1 
          DisableGadget (0,#TRUE)
          DisableGadget (1,#TRUE)
          DisableGadget (2,#FALSE)
          SetGadgetState(3,#TRUE)
          DisableGadget (3,#TRUE)
          DisableGadget (4,#TRUE)
          DisableGadget (5,#TRUE)
        EndIf 
        If EventGadgetID() = 2 
          DisableGadget (0,#FALSE)
          DisableGadget (1,#FALSE)
          DisableGadget (2,#TRUE)
          DisableGadget (3,#FALSE)
          SetGadgetState(3,#FALSE)
          DisableGadget (4,#FALSE)
          DisableGadget (5,#FALSE)
        EndIf 
        If EventGadgetID() = 3 
          If GetGadgetState(3) ; option is selected
            DisableGadget(0,#TRUE)
            DisableGadget(1,#TRUE)
            DisableGadget(2,#FALSE)
            DisableGadget(3,#TRUE)
            DisableGadget(4,#TRUE)
            DisableGadget(5,#TRUE)
          Else  
            DisableGadget(0,#FALSE)
            DisableGadget(1,#FALSE)
            DisableGadget(2,#TRUE)
            DisableGadget(3,#FALSE)
            DisableGadget(4,#TRUE)
            DisableGadget(5,#TRUE)
          EndIf   
        EndIf 
    EndSelect 
  Until Event = #PB_Event_CloseWindow 
EndIf 
End  
Just noticed an anomaly though. Clicking the Read Only button clears the
checkbox gadget while selecting the Option doesn't even though the
code is identical. Hmm...
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Darn. Looks like my brain must have turned to liquid and drained away again. I actually had a bug in my code which was giving me some results I didn't want. Can't believe it. Errr ... never happened before.

Anyway, thanks to everyones help I now have all that I need. In short, 'read only' mode works great for string gadgets with the added bonus that the user can place the mouse cursor on the text field to cut text for copying and can't change text. Perfect. Disablegadget does not, as I previously stated, make the field content disappear but does in fact display the field content and the user cannot place the mouse cursor or tab into that field. It also disables any type of gadget while displaying the content of that gadget. Also perfect. And finally, hidegadget does exactly what it says and hides the field. Between the three commands I can finally achieve what I was trying to get to so am very happy. Thanks to everyone for the help. I am really impressed with this forum and the level of activity in it.

Terry: Interesting anomoly with the checkbox gadget and I have no ideas. Well spotted. I suppose I can always save the value into a variable and set again after changing its state, just to be safe.
Post Reply