Switch multiple StringGadgets from ReadOnly to editable

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by Dude »

thinkitsimple wrote:can i change a flag (#PB_String_ReadOnly) after the StringGadget is created?
I know you want cross-platform, but for Windows only you can do this:

Code: Select all

SendMessage_(GadgetID(gad),#EM_SETREADONLY,state,0)
Where "state" is 1 for locked, or 0 for unlocked. Posting this for future reference for Windows users.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by infratec »

Hi,

as I promised, I tested this on OS X el capitan.
It works:

Code: Select all

EnableExplicit

Define.i Event, Exit


OpenWindow(0, 0, 0, 400, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ContainerGadget(9, 0, 0, 200, 160)
StringGadget(0, 10, 10, 100, 20, "")
StringGadget(1, 10, 40, 100, 20, "")
StringGadget(2, 10, 70, 100, 20, "")
StringGadget(3, 10, 100, 100, 20, "")
TextGadget(5, 10, 130, 100, 20, "DummyText")
CloseGadgetList()

DisableGadget(9, #True)

ButtonGadget(10, 200, 10, 80, 30, "Toggle", #PB_Button_Toggle)


Repeat
 
  Event = WaitWindowEvent()
 
  Select Event
    Case #PB_Event_Gadget
      If EventGadget() = 10
        If GetGadgetState(10)
          DisableGadget(9, #False)
        Else
          SetActiveGadget(5)
          DisableGadget(9, #True)
        EndIf
      EndIf
     
    Case #PB_Event_CloseWindow
      Exit = #True
     
  EndSelect
 
Until Exit
Bernd
User avatar
HeX0R
Addict
Addict
Posts: 1204
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by HeX0R »

Disabling the StringGadget is not the same then ReadOnly mode!
You can still select the text in a ReadOnly StringGadget, but not in a disabled one!

The only possibility to do this without API is to recreate all of the gadgets.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by RASHAD »

It is not that perfect
But I am trying to use all what I can get from PB :mrgreen:

Code: Select all

OpenWindow(0, 0, 0, 400, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

StringGadget(0, 10, 10, 100, 20, "")
StringGadget(1, 10, 40, 100, 20, "")
StringGadget(2, 10, 70, 100, 20, "")
StringGadget(3, 10, 100, 100, 20, "")
EditorGadget(5, 10, 130, 100, 20)
SetGadgetText(5,"For test")

ButtonGadget(10, 200, 10, 80, 30, "Toggle", #PB_Button_Toggle)

Repeat
 
  Event = WaitWindowEvent()
 
  Select Event
    Case #PB_Event_Menu
      Select EventMenu()
        Case 10,11
      EndSelect
           
    Case #PB_Event_Gadget
      Select  EventGadget()
        Case 0 To 3
          If Flag = 1
              SetGadgetAttribute(EventGadget(),#PB_String_MaximumLength,0)
              AddKeyboardShortcut(0,#PB_Shortcut_Delete,10)
              AddKeyboardShortcut(0,#PB_Shortcut_Back,11)              
              ;AddKeyboardShortcut(0,#PB_Shortcut_Left,12)
              ;AddKeyboardShortcut(0,#PB_Shortcut_Right,13)
          Else
              SetGadgetAttribute(EventGadget(),#PB_String_MaximumLength,256)
              RemoveKeyboardShortcut(0,#PB_Shortcut_All)
          EndIf          
          Debug GetGadgetText(EventGadget())
          
        Case 5
            RemoveKeyboardShortcut(0,#PB_Shortcut_All)
          
        Case 10
            If GetGadgetState(10)
              flag = 1
            Else
              flag = 0
            EndIf
      EndSelect
     
    Case #PB_Event_CloseWindow
      Exit = #True
     
  EndSelect
 
Until Exit
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by IdeasVacuum »

..another way would be to have a pair of string gadgets defined in the same place - display the read-only one at first, with the editable one hidden.
In other words: can i change a flag (#PB_String_ReadOnly) after the StringGadget is created?
Nope.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by RASHAD »

Taking HeX0R comment into account
Just modifying Bernd last post a little bit
What do you need from StringGadget() except GetGadgetText()

Code: Select all

;EnableExplicit

Global Flag

Procedure _GetGadgetText(Gad)
If Flag = 1
  DisableGadget(9, #False)  
  Debug GetGadgetText(Gad)
  DisableGadget(9, #True)
Else
  Debug GetGadgetText(Gad)
EndIf
EndProcedure 

OpenWindow(0, 0, 0, 400, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ContainerGadget(9, 0, 0, 200, 160)
StringGadget(0, 10, 10, 100, 20, "")
StringGadget(1, 10, 40, 100, 20, "")
StringGadget(2, 10, 70, 100, 20, "")
StringGadget(3, 10, 100, 100, 20, "")
TextGadget(5, 10, 130, 100, 20, "DummyText")
CloseGadgetList()

ButtonGadget(10, 200, 10, 80, 30, "Toggle", #PB_Button_Toggle)

ButtonGadget(20, 200, 50, 80, 30, "Get #0 Text")


Repeat
 
  Event = WaitWindowEvent()
 
  Select Event
      
    Case #PB_Event_Gadget
      Select EventGadget()
      Case 10
        If GetGadgetState(10) = 0
          DisableGadget(9, #False)
          Flag = 0
        Else
          OldGad = GetActiveGadget()
          SetActiveGadget(5)
          SetGadgetColor(0,#PB_Gadget_BackColor,$ABF6FE)
          DisableGadget(9, #True)
          Flag = 1
        EndIf
      
    Case 20    
      _GetGadgetText(0)
      EndSelect
     
    Case #PB_Event_CloseWindow
      Exit = #True
     
  EndSelect
 
Until Exit
Egypt my love
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by Shardik »

IdeasVacuum wrote:
In other words: can i change a flag (#PB_String_ReadOnly) after the StringGadget is created?
Nope.
Not quite right! I posted already this code example which demonstrates how to toggle the read only attribute of a StringGadget cross-platform... :wink:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Switch multiple StringGadgets from ReadOnly to editable

Post by IdeasVacuum »

Nice example Shardik - given that it is possible via API on all supported platforms, surely PB should allow this natively.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply