Anvil Community Forum

Disable TextBox user input programmatically

Hi all,

Happy Tuesday from sunny Perth.

Is there a simple way to disable user input for TextBoxes and DropDowns on a form temporarily?
I am trying to reuse a data capture form so that I can have two versions of the same form depending on the user’s access level, one is a “read only” version while the second is fully editable.
I have achieved this in another form by duplicating it and replacing the TextBoxes with labels but it is not an efficient solution.

Thanks in advance.

Rick

1 Like

You should be able to set the control’s enabled property to False like this :

self.text_box_1.enabled = False

Works for text boxes and drop downs.

If you need to do a lot of them, you can do something like this (not tested) :

for c in self.get_components():
  if type(c) == Textbox or type(c) == Dropdown:
    c.enabled = False

Make sure to take into account that the controls might be inside containers, for example :

for c in self.column_panel_1.get_components():
  if type(c) == Textbox or type(c) == Dropdown:
    c.enabled = False

or even better, take into account all of the containers :

for c in self.get_components():
  if type(c) == ContentPanel:
    for fc in c.get_components(): 
      if type(fc) in [Textbox, Dropdown]: # Add as many types as you like to the list.
        fc.enabled = False

etc.

edit - updated the last example…

6 Likes

Thanks very much David!
This will save a fair amount of duplication.
Much appreciated.

@bridget A tool tip on the editor for this ‘enabled’ checkbox with the code required would be excellent.
I searched for a while for this but perhaps I had the wrong search terms (looking for disable not enable).

One last one, here’s a sample app showing a recursive disabling function that handles a seed container and then works its way through all sub-containers and their components.

Should be self explanatory, but shout if you want more detail :

https://anvil.works/build#clone:SVNHYEOCCQGYS2ZX=N3K3ZIK55WRSZCMPG3YQ64YL

I reckon the more expert Python people could probably get that down to a one liner … :slight_smile:

2 Likes

Excellent, ill give it a go.

Thanks David!!