B4J Question How to do B4J form stuff (hide, return login form details)


Licensed User
For B4J:

1. Is it possible to hide an existing form before opening a new form? I don't mean closing a form but "hiding" it until some later time when you want to "unhide" it.

2. How do you "pass" data between forms without using globals?

3. I am currently creating a new code module for each form, with a "show" method (like in the Threepages example at https://www.b4x.com/android/forum/threads/getting-started-examples.57537/#content )
This allows me to do FormCodeModuleName.Show to display the new form. I like this because it keeps each form's code separate, but is there any disadvantage to this approach?

4. It's a general question but what is a suggested way to handle communication between different forms & main? For example, having a reusable "login" dialog that can be called from different modules/main and returns username & password that were entered?

5. Is there a "real life" example of such code that anyone could share? (Sometimes it's the simple things that give me problems...)



Staff member
Licensed User
1. Form.Close will hide the form. You can later call Form.Show to show it. Closing the main form will end the app. There are simple ways to prevent it.

2. Forms are regular objects like any other objects. You do not pass data between forms. You pass data between modules or classes. Just call the sub you like with the data you need to pass.

3. See the three pages example: https://www.b4x.com/android/forum/threads/getting-started-examples.57537/#content


Licensed User
1. Form.Close will hide the form. You can later call Form.Show to show it. Closing the main form will end the app. There are simple ways to prevent it.
1. If the app closes when MainForm.Close is done, how would you hide MainForm then? (This would seem to mean that Main Form cannot be hidden?)

2. If I have a form (MainForm) that has a layout loaded (for example "Splash screen" layout) then I load another layout ("Main Menu" layout) the 2nd is shown displayed "over" the 1st? (i.e. you can see both layouts combined). What do I have to do to "clear" the MainForm if I wanted to display another layout in the same form?



Licensed User
Maybe this will help you understand
Thanks, this has been useful for figuring out how to programmatically add controls to a form.

I am still trying to figure out a few things:

1. Where should a form's code reside? In VB6/VB.NET code is "behind" forms. (You create forms and then attach code to them). However, with B4J it seems forms are created (or layout loaded) with code? (i.e. Code runs first, forms created or managed as a result?)

2. What is the best practice for organizing form code and modules? Do you dump all form code in main or create code modules and manage each form from a different module?

3. In the code (from Daestrum's example) below, how come the app doesn't end when MainForm.Close? Does the app remain open because there is another form (loginForm) open before MainForm.Close?

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("splash") 'Load the layout file.
   b.Text = "Press to login"
4. In the above code, why do both the event handler for b.Initialize("login") and loginForm.Initialize("login",200,200) have the same name? Wouldn't you want different names to avoid confusion? (especially if both objects have same possible events) What would happen if both the button and the form had same named events?

Sorry for all the questions but B4J/JavaFX is a little different to what I have experience with and am trying to understand.


Well-Known Member
Licensed User
4, yes you are correct I should have named them differently - my bad.

3, in this sub
Sub mainform_CloseRequest (EventData As Event)
 If loginForm.Showing Then ' if loginForm showing don't exit application
 End If
End Sub
I check if the loginForm is visible on the desktop, if it is I consume the close eventdata, effectively refusing to close the mainform whilst the loginForm is about.

2 & 1 I leave for someone with better explanation than mine (code lives where I write it - not really helpful to you).