B4J Question B4J Add View at runtime [solved]

Didier9

Well-Known Member
Licensed User
Longtime User
I probably missed it but I have not found how to create and add views at runtime in B4J.
As always, any help deeply appreciated.
 

DonManfred

Expert
Licensed User
Longtime User
Seriously?

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    Dim p As Pane
    p.Initialize("Pane")
    Dim b As Button
    b.Initialize("Button")
    p.AddNode(b,10dip,10dip,75dip,50dip)
    MainForm.RootPane.AddNode(p,0,0,100dip,200dip)
    MainForm.Show
End Sub
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Thank you, seriously.
Not a lot of experience with B4J.
Missed the change from "View" to "Node" in Beginner's Guide chapter 5.9
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
This is a more functional approach. Takes care of the position. Toggle the visibility on / off - as desired...
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Yes but I have an array of 44 labels (don't ask, it's a rewrite of an old Visual Basic software that has to work the same way) and they all have the same width, height, font and such but the content changes in real time (it displays the status of remote devices over serial), so doing it by hand is not an option. The only practical way to do it is programmatically. Even if it did not need to change at run time, it's much easier to adjust the positions or spacing. It's just a For loop.
I started by hand and did the first 22. That's when I went on the forum :)

I do use the layout for isolated controls, but when dealing with arrays of views, it's much easier to do it programmatically.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
It is actually quite functional when done and we depend on that old software a lot where I work. The problem is that I was hanging to the VB 6.0 code because I had not found anything else I liked to rewrite the old code in. Now that my company has put a lock on Windows 10 and thrown away the key, I can no longer install the old VB 6.0 code so I decided to give B4J a try.
So far, I am pretty pleased. I was able to emulate most of the VB functionality I needed, and I am no longer limited to COM16 for serial ports. I have already done one tool (a CAN decoder) and now I am doing the biggie...
And of course it installs everywhere.
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private NumberOfLabels As Int = 7
    Private labels(NumberOfLabels, NumberOfLabels) As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UTILITY")
    MainForm.Show

    For x = 0 To NumberOfLabels - 1
        For y = 0 To NumberOfLabels - 1
            Dim lb As Label
            lb.Initialize("btn")
            lb.Text = $"Label ${x}, ${y}"$
            lb.Alignment = "CENTER"
            labels(x, y) = lb
            MainForm.RootPane.AddNode(lb, 0, 0, 10, 10)
        Next
    Next
End Sub

Private Sub MainForm_Resize (Width As Double, Height As Double)
    Dim LblWidth As Double = (Width - 10)/ NumberOfLabels
    Dim LblHeight As Double = (Height - 10)/ NumberOfLabels
    For x = 0 To NumberOfLabels - 1
        For y = 0 To NumberOfLabels - 1
            labels(x, y).SetLayoutAnimated(0, 5 + x * LblWidth , 5 + y * LblHeight , LblWidth - 1, LblHeight - 1)
        Next
    Next
End Sub
 
Upvote 0
Top