B4J Question [B4X] [SOLVED] Duplicate, copy B4XView... to an array of b4xviews (is it possible-->yes it is)

Magma

Expert
Licensed User
Longtime User
Well,
I hate myself when i fall/fail to the "same" errors...

But I have to say that same things at same language are many (i think for compatibility) - so i want to refresh old apps and some brain-ideas not working with new XUI B4X things :-( - or what...

So for example i have one circle at one b4xview and i want to copy to 15 new !! and this time need to do that at programmatically/runtime not at layout / using loadlayout..

my code:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private piece As B4XView
    Dim b4xc As B4XCanvas
    Dim blackpieces(15) As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
   
    b4xc.Initialize(piece)
    b4xc.DrawCircle(20,20,15,xui.Color_Black,True,1)
   
    For i=0 To 14

    blackpieces(i)=xui.CreatePanel("")
    blackpieces(i)=piece
    blackpieces(i).BringToFront
' blackpieces(i).left=200 ' hey this not works too... why ?
    blackpieces(i).SetLayoutAnimated(200,Rnd(50,900),Rnd(50,600),40,40)

       
   
    Next
   
End Sub

Thinking that all good ?

but nope... something going wrong... only one showing - moving - i think the last...

why ?

1) How i will copy to 15 new pieces ---> blackpieces ???
+and a 2nd question too: ' blackpieces(i).left=200 ' hey this not works too... why ? - and must use setlayoutanimated to move views...

ps1: As I am thinking - I wonder this - is it real possible to create b4xview on runtime... is it ok with xui.createpanel ... or addview is better.... (2 more questions...)
ps2: ...why am i refreshing my apps ? ?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
All the 15 array elements are pointing to the same panel (the one on your layout). You have to create 15 canvases associated with 15 panels.

B4X:
    'If not B4XPages then MainForm.RootPane.AddNode(blackpieces(i).As(Pane), Rnd(50,900), Rnd(50,600), 40, 40)
    'Label1 is just a label in myPieceLayout, I gave it some color and a border for clarity
    For i = 0 To 14
        blackpieces(i) = xui.CreatePanel("")
        Root.AddView(blackpieces(i), Rnd(50,900),Rnd(50,600), 40, 40)
        blackpieces(i).LoadLayout("myPieceLayout")
        Sleep(10)            'need a little time to load layout
        label1.SetLayoutAnimated(0, 0, 0, 40, 40)
        b4xc.Initialize(blackpieces(i))
        label1.SendToBack
        b4xc.DrawCircle(20,20,15,xui.Color_Black,False,1)
        b4xc.Invalidate
    Next
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
You are right - ofcourse ! - Thanks a lot !

All the 15 array elements are pointing to the same panel (the one on your layout). You have to create 15 canvases associated with 15 panels.

B4X:
    'If not B4XPages then [B][U]MainForm.RootPane.AddNode(blackpieces(i).As(Pane), Rnd(50,900), Rnd(50,600), 40, 40)  <---------That was i looking !!![/U][/B]
    'Label1 is just a label in myPieceLayout, I gave it some color and a border for clarity
    For i = 0 To 14
        blackpieces(i) = xui.CreatePanel("")
        Root.AddView(blackpieces(i), Rnd(50,900),Rnd(50,600), 40, 40)
        blackpieces(i).LoadLayout("myPieceLayout")
        Sleep(10)            'need a little time to load layout
        label1.SetLayoutAnimated(0, 0, 0, 40, 40)
        b4xc.Initialize(blackpieces(i))
        label1.SendToBack
        b4xc.DrawCircle(20,20,15,xui.Color_Black,False,1)
        b4xc.Invalidate
    Next
 
Upvote 0
Top