How to send a textbox value on Form1 to a textbox on Sub-form2?

Greetings,

Main Form1 is named “home” and the sub-form2 is named “doublelug”. The objective is to send (via button) a value in a textbox located on the “home” form to a textbox located on the “doublelug” sub-form. I have tried importing the “doublelug” form on the “home” form to get access to the “doublelug” textbox in order to update the value using button on the “home” form; however, this fails to update value even though I have access to the “doublelug” textbox from the home form. Respectively request coding suggestions that will achieve the objective. Thanks!

There are two ways to achieve this. Here is one of them:

home

open_form('doublelug', "test" ,)

doublelug

var1 = 0

def __init__(self, var1,  **properties):
   self.var1 = var1

How are you adding the form “Doublelug” to the Home form?

If you are dragging it onto the form in the designer, then you will need to reference it by the name. The IDE will give it a name of something like “custom_1” by default.

Then in your button click event you would do something like :

self.custom_1.text_box.text = "Clicked"

Here’s an example you can clone and dissect and adapt :
https://anvil.works/build#clone:Z4G7H475ONJSXRXT=OKOH7G73Q3GJ4LDDEOGULMQ7

1 Like

The original message suggests that there may be some confusion between classes and instances.

  1. import provides access to a the Form’s class.
  2. But values reside in the class's instances.

I suggest making sure to clearly identify the instances under discussion.

Hi David,

To answer your question…I simply added the doublelug form by right-clicking on home form (I did not drag). See image…and thanks for your timely feedback!

anvil_tree

Ok, I see. The form hierarchy in the IDE is organisational. You haven’t created a sub-form as such. You still need to “show” the form or import it to create an instance of the form to manipulate.

So, how do you want the doublelugui form to be displayed - on it’s own or embedded in the Home form? If embedded, do you want to display it dynamically in code or permanently by adding it at design time?

If you import it you will still need to create an instance. For example :

from .doubleLugUI import doubleLugUI
...
# This creates the instance of the form.
new_form_instance = doubleLugUI() 
# You should now be able to reference controls on that instance.
new_form_instance.textbox.text = "hello"

This is off the top of my head and not tested, but that’s broadly how you import a form and use it.

Specific advice will depend on your intended method to show the form.

Trust the autocomplete when using import. After typing from and the first dot, it should give you a menu of choices to import. It’s usually the safest way to import.

edit - I have updated the demo (see clone link above) to show this technique in action.

1 Like

The intent is to show the doublelugUI form on its own. It is launched via button click (from home), the end-user then populates required textboxes. doublelugUI is closed and user is sent back to home. It is there that the update to one of the doublelugUI textboxes is needed with the doublelugUI form closed (or not active). I would prefer not having to embed the doublelugUI into the home form…if possible.

I tried the code above and it was unsuccessful…note, the doublelugUI was not embedded in the home form when I tried your code.

Agreed. See also

Worth noting that you can also pass a form instance to open_form as opposed to the form name (useful since you don’t want to embed forms but want to keep references of alive forms)

So you just need to work out how to keep a reference to your instances of home and doublelug alive and accessible.

You could adapt the code above

from ..DoubleLug import DoubleLug
...
# This creates the instance of the form.
self.doublelug = DoubleLug(home=self) 

Now inside the init method of DoubleLug

def __init__(self, home, **properties):
  self.home = home
  self.textbox.text = self.home.textbox.text

def close_button_click(self, **even_args):
    open_form(self.home) 
    # open the instance of Home that we kept alive 

So now doublelug has a reference to the current home form and can access all of its components.

When you call open_form. Instead of calling it with a string call it with these instances.

If you want to create a new version of doublelug each time the button is clicked you can also pass parameters to open_form - something like.

def button_click(self, **event_args):
    open_form('DoubleLug', home=self)
    # or just like above keep the reference to doublelug alive
    # open_form(self.doublelug)

You could also consider moving these references to a separate module that contains a cache. And each time you want to create an instance you check the cache first to see if that form already exists.

2 Likes

Absolutely freakin genius!! Worked…the key of course was keeping the reference to doublelug alive…now when clicking on the button on the home form with the desired value, the value in the textbox located on the doublelug form updates - perfect! Could not have done it without your help stucork - good job! Shout out to david.wylie for his contributions and timely assistance as well. :+1:

1 Like